guide.org 2.7 KB

Syntax

The schedule specification syntax is:

minute hour day month weekday

Logs

General log files

General logs of crontab can be viewed using the following command:

grep CRON /var/log/syslog

This probably requires to have rsyslog installed.

If no logs show up, check the file /etc/rsyslog.d/50-default.conf for a commented out line like #cron.* and uncomment it. Then restart rsyslog using service rsyslog restart.

Logging each job

Variant 1 - redirect stderr and output to log file

0 13 * * * call_to_script >> /home/log/myscript.log 2>&1

Variant 2 - redirect stderr and stdout to separate files

42 17 * * * command >>stdout.log 2>>stderr.log

Variant 3 - redirect and display using tee

0 13 * * * call_to_script 2>&1 | tee --append log_file 2>&1

This has the advantage, that the cronjob is observable, when run manually.

Variant 4 - redirect and display without depending on tee in docker containers

0 9 * * * command > /proc/1/fd/1 2>&1

The /proc/1/fd/1 is explained as follows: /proc/PID/fd/1 where /proc is a conventional location, then follows the PID of the process whose file descriptors will be used, then /fd, which stands for "file descriptors" and then /1 for stdout or /2 for stderr.

Docker always uses the stdout from process with PID 1 as the docker log stream, so redirecting output to /proc/1/fd/1 will make the output visible in docker logs ... output.

Variant 5 - redirect and display without depending on tee

0 9 * * * command > /proc/$(cat /var/run/crond.pid)/fd/1 2>&1

The /proc/1/fd/1 is explained as follows: /proc/PID/fd/1 where /proc is a conventional location, then follows the PID of the process whose file descriptors will be used, then /fd, which stands for "file descriptors" and then /1 for stdout or /2 for stderr.

$(cat /var/run/crond.pid) will find the PID of the running cron process.