How to stop cron from mailing you

terminal.png

Running numerous jobs via the cron daemon on a daily, even hourly, basis can lead to a lot of mail notifications. I run many jobs whose output I do not really need to be informed of (for example, the overnight stats run on some of my web servers; I am in the habit of checking stats daily and if the job failed I would know), so how do I go about preventing crond telling me about these jobs?

There are a couple of ways of achieving this. When running a job, crond will look at stdout and stderr and send what it sees to the user(s) specified in the MAILTO environment variable, or if MAILTO is not defined it sends the output to the owner of the cron job.

One way of preventing crond from mailing you the results of stderr and stdout is to send them to /dev/null as the job runs. Appending > /dev/null to the cron job entry will redirect stdout to /dev/null. But what of stderr? Well, redirecting stderr to stdout (who is already redirected to /dev/null) will achieve the desired goal. So appending > /dev/null 2>&1 will redirect both stdout and stderr to /dev/null.

The 2>&1 part essentially breaks down as “redirect file descriptor 2 to the location pointed to by file descriptor 1?

So, a line in your crontab where you wish to redirect output to /dev/null might look like:

30 3 * * * /bin/sh /home/u/somescript > /dev/null 2>&1

Above we mentioned the MAILTO environment variable. However, there was a case in relation to this variable that we did not mention. That is, if MAILTO is set but empty. In this instance, crond will send no mail.
Source

Related Posts

Comments are closed.