Question: How can I stop receiving email notifications from cron jobs? How do I prevent wget from creating a new file on each run? Why am I getting so many emails from crontab to my root account?
In Linux, when executing cron jobs, it’s typical to get emails or log files as output. There are times, though, when you might prefer not to get any output, particularly if the job runs often or produces substantial output. This guide will explain how to stop crontab output on Linux.
1. Redirect output to /dev/null
A simple method to stop crontab output is to send it to /dev/null
. This is a unique file that ignores all data sent to it. Sending output to /dev/null
allows you to discard any output from the cron job.
To send output to /dev/null, include this line in your crontab:
* * * * * command > /dev/null 2>&1
This command redirects both standard output and standard error to /dev/null
, thus stopping all output from the command.
This method is particularly helpful for cron jobs that run the wget command. For example, I had a cron job that used wget every minute, creating a new file each time it ran. By configuring it as shown below, I stopped the file accumulation in my home directory:
0 2 * * * wget -q -O /dev/null http://example.com/cron.php
2. Adjust the MAILTO Environmental Variable
Cron jobs typically send email notifications by default. However, you can prevent these emails by setting the MAILTO environmental variable to an empty string.
For stopping email alerts for a specific cron job, insert this line in your crontab:
MAILTO=""
This action will assign an empty value to the MAILTO
variable, thus stopping email alerts for that particular job.
3. Change the Cron Daemon Settings
To globally disable crontab output for all jobs on your system, consider altering the cron daemon’s settings. The default behavior of the cron daemon is to send email notifications for all tasks, but this can be changed.
For this adjustment, you need to edit the /etc/crontab
file and add:
MAILTO=""
By doing this, you set the default MAILTO value to an empty string, which stops email notifications for all jobs.
Conclusion
This article provides various strategies for disabling crontab output in Linux. Whether you opt to redirect output to /dev/null
, adjust the MAILTO
variable, or modify the cron daemon’s configuration, it’s crucial to thoroughly test your changes. This ensures that your cron jobs function as intended. Employing these methods, you can better manage the output and notifications from cron jobs on your Linux system.
4 Comments
doesnt work i keep seeing this in syslog every minute: “wget http://example.com/cron.php >/dev/null 2>&1″
Why is >/dev/null 2>&1 appended to the line after MAIL TO=”[email protected]” ?
Thanks! I had over 150.000 files in my home directory from a cronjob running wget every two minutes.
Thank you Mr. Rahul. It’s very helpful