Configure log in crontab

1

I use crontab as follows.

0 * * * * wget -q -O /var/www/CronTab.txt https://www.meuseite.com.br/tarefa.php

So it always creates a txt file 'CronTab.txt' with the result, and if it has any errors it will write to the file.

The problem is that whenever it runs it creates the blank file and deletes the old file.

How do I create the file without deleting the old one? That is to add the result at the end of the file.

    
asked by anonymous 21.02.2018 / 15:12

1 answer

2

According to documentation , to attach to the log file without replacing the previous one should use the parameter -a , it does the same as the parameter -o only instead of replacing the log of the file with generated, it appends to the old one.

wget http://www.example.com -a Logs.log

If the logs are already in the logslog file , the log file will be created.

EDIT

You can use cURL , in the simple example below, I've created a .sh file. >:

#!/bin/sh
STATUS=$(curl -s -o /dev/null -w '%{http_code}' https://pt.stackoverflow.com/naoexiste)
DATA=$(date '+%d/%m/%Y %H:%M:%S')
if [ ! $STATUS -eq 200 ]; then
    echo -e "$DATA - $STATUS" >> /var/www/logs/Logs.log
fi

Explanation

Parameters:

We run a condition that checks whether the value contained in the STATUS variable is different from 200 , otherwise it saves the status in the log.

To schedule the task, simply:

* * * * * /home/USUARIO/executa_tarefa.sh

Reference

21.02.2018 / 17:08