Configure log in crontab

Asked

Viewed 232 times

1

Good use of 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 you have any error it will write to the file.

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

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

1 answer

2


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

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

Doing so the generated logo if attached to the file Logs.log if it exists, otherwise the file will be created.

EDIT

You can use Curl, in the simple example below, I created a file .sh:

#!/bin/sh
STATUS=$(curl -s -o /dev/null -w '%{http_code}' /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:

  • -s or --Silent

    Runs in silent mode.

  • -the or --output

    Writes the output to a file instead of stdout.

  • -w or --write-out

    The variable reported in this parameter will be replaced by the value or text as described in the documentation.

We perform a condition that checks whether the value contained in the variable STATUS is different from 200, if different save the status in the log.

To schedule the task is enough:

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

Reference

  • Amigo sa sua forma ficou muito bom, however I think the log will be very big because it is registering even when the file is empty (when there will be no error). It’s like he only records if there’s a mistake?

  • Okay, thanks for the help.

  • Friend just one more detail, the command wget http://www.example.com -a Logs.log works perfectly, but is creating an empty file in more server, knows how to solve it?

  • Look at the modification I made.

  • Very good, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.