Cronjob – How to Completely Silence a Cronjob to /dev/null?

cronemailio-redirection

On my Ubuntu-Desktop and on my debian-server I have a script which needs to be executed each minute (a script that calls the minute-tic of my space online browsergame).

The problem is that on debian derivates cron is logging to /var/log/syslog each time it executes. I end up seeing repeated the message it was executed over and over in /var/log/syslog:

Nov 11 16:50:01 eclabs /USR/SBIN/CRON[31636]: (root) CMD (/usr/bin/w3m -no-cookie http://www.spacetrace.org/secret_script.php > /dev/null 2>&1)

I know that in order to suppress the output of a program I can redirect it to /dev/null, for example to hide all error and warning messages from a program I can create a line in crontab like this

* * * * *       root    /usr/local/sbin/mycommand.sh > /dev/null

But I would like to run a cronjob and be sure that all generated output or errors are piped to NULL, so it doesn't generate any messages in syslog and doesn't generate any emails


EDIT:
there is a solution to redirect the cron-logs into a separate log like proposed here by changing /etc/syslog.conf

But the drawback is, that then ALL output of all cronjobs is redirected.

Can I somehow only redirect a single cronjob to a separate log file? Preferably configurable inside the cron.hourly file itself.

Best Answer

Make the line this:

* * * * *       root    /usr/local/sbin/mycommand.sh > /dev/null 2>&1

This will capture both STDOUT (1) and STDERR (2) and send them to /dev/null.

MAILTO

You can also disable the email by setting and then resetting the MAILTO="" which will disable the sending of any emails.

Example

MAILTO=""
* * * * *       root    /usr/local/sbin/mycommand.sh > /dev/null 2>&1

MAILTO="admin@somedom.com"
 * * * * *      root    /usr/local/sbin/myothercommand.sh

Additional messaging

Often times you'll get the following types of messages in /var/log/syslog:

Nov 11 08:17:01 manny CRON[28381]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)

These are simply notifications via cron that a directory of cronjobs was executed. This message has nothing to do directly with these jobs, instead it's coming from the crond daemon directly. There isn't really anything you can do about these, and I would encourage you to not disable these, since they're likely the only window you have into the goings on of crond via the logs.

If they're very annoying to you, you can always direct them to an alternative log file to get them out of your /var/log/syslog file, through the /etc/syslog.conf configuration file for syslog.

Related Question