Redirecting output of cron jobs

Cron jobs are very useful (indispensable I would say), but a common issue people have with cron jobs is that they are getting mails about errors produced by the scripts run by cron jobs. Also, the output is sent to syslog and this can be a serious problem when you have a script that executes often and produces long error messages.

Simplest solution to this problem is to redirect output of a cron job to /dev/null, which is a sort of a black hole that discards anything written to it. To do this, set you cron jobs like this:

* * * * * /home/user/ > /dev/null 2>&1

This will redirect both STDOUT (1) AND STDERR (2) to /dev/null.

Sometimes however you want to redirect output to a log file of your choice. In that case, the code would look like this:

* * * * * /home/user/ >> /var/log/custom/my.log 2>&1

Note that this time we are using “>>” instead of “>” for redirection. The important difference between the two redirection operators is that “>” will overwrite data each time, while “>>” will append them to the file.

Leave a Reply

Your email address will not be published. Required fields are marked *

Reload Image