Many Linux administrators encounter this frustrating scenario: you've set up a cron job with output redirection to /dev/null
, yet your root mailbox keeps getting flooded with execution notifications. Let's examine why this happens and the proper solutions.
In your case with the Apache restart script:
* * * * * root /bin/apache2-restart.sh &> /dev/null
The &> /dev/null
redirects both stdout and stderr to null, but cron still sends mail when:
- The command produces any output (not properly silenced)
- The command exits with non-zero status
- cron's MAILTO variable is not properly configured
Method 1: Proper Output Redirection
The most reliable approach combines all possible redirections:
* * * * * root /bin/apache2-restart.sh >/dev/null 2>&1 || true
Method 2: Disable Mail Globally
Add this to your crontab:
MAILTO=""
* * * * * root /bin/apache2-restart.sh
Method 3: Configure Null Mailer
For system-wide configuration:
# Install nullmailer
sudo apt-get install nullmailer
# Configure postfix to discard mail
sudo dpkg-reconfigure postfix
For your Apache restart script, check these potential issues:
- The script might be producing output before your redirection takes effect
- There could be wrapper scripts or functions that generate output
- Permissions might cause the redirection to fail
For critical cron jobs, consider logging instead of complete silencing:
* * * * * root /bin/apache2-restart.sh >> /var/log/apache_restart.log 2>&1
Combine with log rotation to prevent disk space issues:
# /etc/logrotate.d/apache_restart
/var/log/apache_restart.log {
weekly
rotate 4
compress
missingok
notifempty
}
Many Linux administrators encounter situations where cron jobs keep sending email notifications to the root user despite output redirection. The example shows a typical case:
* * * * * root /bin/apache2-restart.sh &> /dev/null
Even with &> /dev/null
redirecting both stdout and stderr, emails continue flooding the root mailbox. This happens because cron's mailing behavior operates independently of shell redirection.
Cron sends email when:
- The command produces any output (stdout/stderr)
- The command exits with non-zero status
- MAILTO variable isn't properly configured
The redirection in your command only prevents output from reaching the terminal - cron still sees the command's execution status.
Here are three reliable methods to stop cron emails:
1. Full Output Suppression
Modify your cron entry to:
* * * * * root /bin/apache2-restart.sh >/dev/null 2>&1 || true
The || true
ensures the command always exits with status 0.
2. MAILTO Variable
At the top of your cron file:
MAILTO=""
* * * * * root /bin/apache2-restart.sh
Empty MAILTO completely disables email notifications.
3. Wrapper Script Solution
Create a wrapper script:
#!/bin/sh
/bin/apache2-restart.sh >/dev/null 2>&1
exit 0
Then call the wrapper from cron.
For Ubuntu systems, edit /etc/rsyslog.conf
:
# cron.* /var/log/cron.log
Then restart rsyslog:
sudo service rsyslog restart
After implementing changes:
- Wait for the next cron cycle (or force run with
sudo run-parts /etc/cron.d
) - Check root's mail:
sudo mail -u root
- Verify system logs:
sudo grep CRON /var/log/syslog