So one of your file systems is full and you traced it to a curious phenomena: root’s mailbox is loaded with messages. And you think to yourself, “Who the hell is sending mail to root?” Chances are, it’s you!
This recently happened to a friend who’s new to Linux. Root’s mailbox was full of messages that looked like this:
/bin/sh: ntpdate: not found
It turns out he had a cron job that ran ntpdate to sync his system time with a server at the National Institute of Standards. (Here’s a complete list of NIST time servers.)
The program ‘ntpdate’ was located in /usr/bin which was not in root’s PATH. So here is lesson number one: Always use the full path to a program in cron. In this case, he should have used /usr/bin/ntpdate.
Now about those mail messages. When a cron job fails, it captures the program’s output and mails it to the cron owner. If you set /bin/ls to run every five minutes in cron, you’re going to get a directory listing in your mailbox every five minutes. You can silence cron and stop mail messages by redirecting output to /dev/null. The ‘>’ character means ‘redirect’ Here’s the syntax: /bin/ls > /dev/null
Yet that wouldn’t have helped Your JoeDog’s friend. In his case, the offending message was an error message. Error messages are written to a different file descriptor.
The Linux/UNIX shell has three file descriptors: stdin, stdout and stderr. ‘/bin/sh: ntpdate: not found’ was written to stderr. In the shell, 1 represents stdout and 2 represents stderr. To capture both we need another character. ‘&’ means file descriptor. So 2>&1 means redirect stderr (2) to whatever stdout (1) points to. Here’s the syntax he should have used:
0 */6 * * * /usr/bin/ntpdate nist1-pa.ustiming.org > /dev/null 2>&1
Now Your JoeDog has been in this game for quite a while. In the day, to really silence the crontab we’d write the redirect like this:
0 */6 * * * /usr/bin/ntpdate nist1-pa.ustiming.org 1 >/dev/null 2>&1
That’s basically the same as the first one but it seemed to work around oddities and keep our mailboxes clean. The crontab man page still contains that syntax. Your JoeDog will continue to use that superfluous first 1 but your mileage may vary.