How to stop CRON generating email output

Discussion about BCA's Internet Hosting Service
User avatar
David Gibson
Posts: 482
Joined: Thu 16 Mar 2006 23:45

How to stop CRON generating email output

Post by David Gibson » Sun 06 Jan 2019 14:34

If you have selected CPANEL's setting to send you an email when your CRON jobs execute, and you want to disable this for specific jobs, CPANEL says...

You can have cron send an email everytime it runs a command which produces output. If you do not want an email to be sent for an individual cron job you can redirect the command’s output to /dev/null like this: mycommand >/dev/null 2>&1

That is not the whole truth - although it is repeated through the Internet. The salient point is that an email will be generated if CRON does not know what else to do with the output, so sending STDOUT and STDERR anywhere will suppress email output for that output stream. Thus, for example mycommand >/some_file will generate an email if there is any output on the STDERR stream, and mycommand >/some_file 2>some_error_file will log all output and not send an email.

Note that some commands (e.g. CURL) will produce output on STDERR which is not what the naive user might think of as an "error reports". Thus, if you want STDOUT to send you an email, you might want to redirect STDERR somewhere else, otherwise the two streams will be mixed in your email.

Ive taken to using CRON in the form curl http://somefile?a=1\&b=2 >mylog 2>myerrorlog because that provides an easy way to test the command, via a browser, and then simply log the web page it produces. TOP TIP: Dont forget to to escape any &s in your query string. If you dont do that, your log files will be empty and CRON will send the output by email. (The ampersand is treated as a command terminator and means 'run this command in the background').

For more on > and 2>&1, etc, go to https://www.gnu.org/software/bash/manua ... tions.html or http://man7.org/linux/man-pages/man1/ba ... EDIRECTION. The latter is better formatted, so its easier on the eye, but neither are a particularly easy read. But there are loads of people asking about redirection and so Google will point you somewhere helpful, probably.