The crontab command is used to maintain crontab files for individual users. By default the output of a command or a script (if any produced), will be email to your local email account. To stop receiving email output from crontab you need to append following strings at the end of crontab entry.
Cron job prevent the sending of errors and output
To prevent the sending of errors and output, add any one of the following at the end of the line for each cron job to redirect output to /dev/null.
>/dev/null 2>&1
OR
> /dev/null
OR
> /dev/null 2>&1 || true
Cron job example
Edit/Open your cron jobs, enter:
$ crontab -e
Append string >/dev/null 2>&1 to stop mail alert:
0 1 5 10 * /path/to/script.sh >/dev/null 2>&1
OR
0 1 5 10 * /path/to/script.sh > /dev/null
OR
0 * * * * /path/to/command arg1 > /dev/null 2>&1 || true
Save and close the file.
Set MAILTO variable
You can set MAILTO=”” variable at the start of your crontab file. This will also disable email alert. Edit/Open your cron jobs:
$ crontab -e
At the top of the file, enter:
MAILTO=""
Save and close the file.
This entry is 8 of 15 in the Linux Cron Jobs Howto & Tutorial series. Keep reading the rest of the series:
- What is cron on a Linux or Unix-like systems?
- HowTo: Add Jobs To cron Under Linux or UNIX?
- Linux Verify crond Daemon And Cronjobs Are Running
- Linux Start Restart and Stop The Cron or Crond Service
- Linux: List / Display All Cron Jobs
- Linux / UNIX Crontab File Location
- Linux / UNIX: Change Crontab Email Settings ( MAILTO )
- Disable The Mail Alert By Crontab Command On a Linux or Unix-like Systems
- Linux: At What Time Cron Entries In cron.daily, cron.weekly, cron.monthly Run?
- Linux Execute Cron Job After System Reboot
- Linux / UNIX Setup and Run PHP Script As A Cron Job
- How to run crontab job every minute on a Linux or Unix-like system
- Run crontab (cron jobs) Every 10 Minutes
- Cron Job Script Execution on the Last Day of a Month
- Run crontab (cron jobs) every 1 minute
I tried above thing and it works on my server
Thanks.
-Renish
Setting
MAILTO=""
in your crontab disables the sending of emails aswell.If your crontab has huge number of scripts to run it would be cumbersome to append >/dev/numm 2>&1 to each line. Like I have 369 scripts in my crontab. So I find it better to have the MAILTO=”” line at the start of my crontab instead.
Hahaha just stop there for a while lol….
Holy cow. That is so many scripts for a crontab…
But if you have so many cron jobs and you want disable mail alert for a few of them, while other jobs needs a mail alert, then &>/dev/null would be the best choice.
Thanks
For the csh scripts, at least on every system I have access to, to redirect stderr, you need to put >& /dev/null after the command, not &> as indicated by the note.
I used your tip for my openads installation, thanks, I think cronjobs are pretty complicated things
If you want the crontab to run daily, weekley, monthly etc.. a good shortcut is to use the variables
@daily, @weekley etc…
It saves you accidently missing out a * and getting thousands of emails by mistake
http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab+5
for a full list of them
I try MAILTO environment var and work ok. Thanks for help !
Is it MAILTO or EMAILTO?
Only MAILTO worked for me. I think EMAILTO is wrong.
@ slowpoison
Thanks for the heads-up.
It’ll be better if you redirect only the std output to /dev/null instead redirecting both (stdout & stderr). This way only commands with failure exit status will be delivered.
* * * * /path/to/script.sh > /dev/null
regards,
Thank you Edward,
your “> /dev/null” was the only option that did the job for me. I run and adminster 3 servers with OpenBSD 4.3-5 and the cron did not like any other stuff but what you wrote. Thanks a lot. You saved me a lot of work.
Peter
It is not required to restart cron to effect changes to your crontab. Each time cron wakes up, it checks to see if the crontab has changed, and if so cron reparses it.
Thank you OP and Anand Sharma
I would like to know where I can adjust the email address of cron’s mail? Apparently I set an email address somewhere when I installed the server, but I’m unable to find where I can adjust that email address. I’m running debian lenny. Can someone help me here?
@Frank
Sure, use the MAILTO=”” trick, just don’t put “”
MAILTO=”frank@example.com”
Hi Vivek, great tip, thank you!
The correct way to restart crond in HP-UX is:
# /sbin/init.d/cron stop
# /sbin/init.d/cron start
Best regards.
Hi,
I wrote to the top of my crontab file
MAILTO=”myadress@gmail.com”
and the outputs of commands are not coming to my gmail adress. Why?
Thank you.
@IIker, Have a look at your
/etc/postfix/sender_canonical
/etc/aliases
and reload with
/etc/postfix/postmap sender_canonical
/etc/postalias aliases
with aliases you can send mails to root and other addresses without putting it in the MAILTO in crontab. That’s how I was told to do it and it works on my servers.
MAILTO=”” is the best solution :P
Thanks for the tip ;)
Will this work for the at command as well?
I’m trying to setup a series of commands that check for a condition and then reschedule themselves until it’s not true. The problem is every run sends an email to root, I need to stop that.
Thanks for the tip, works great to disable cron emails.
Hi Vivek Gite,
Thank you for your post.
You can use several times the MAILTO variable in order to enable/disable e-mail sending to specific CRON job.
Cheers,
Mickael
Works like a charm. Thanks for sharing.
Hi,
I have another question for you. I want to set a cronjob which should be doing following:
checking a error file if generated every night in a specific directory and if there is one then sending to 5 users email id?
I think this should be done by creating a shell script and scheduling it every night.
Thanks a lot
Hi Rick,
I must say, it is not really a question about CRON, you are asking for writing a bash script?
Could you give your current scipt and if you have an issue with CRON notification, you could details your problem.
Cheers,
Mickael
@Michael
Thanks for replying here it is
#!/bin/sh
err_log_dir=”/apps/Load/DataLoad/DataLoadLogs”
logfile=”/apps/Load//DataLoad/DataLoadLogs/Mail_Log.log”
count=$(ls -1 ${err_log_dir}/*.err 2>/dev/null | wc -l)
if [ $count != 0 ]
then
echo “ERROR: There was an error in loading data” >> ${logfile}
MAILTO=”xysz.com” >> ${logfile}
else
echo “No Error file was created.” >> ${logfile}
exit
fi
Hi,
Any one can reply to my above query?
Thanks
Try ‘if [ ${count} -ne 0 ] ; then ….
How can i surpress certain
Output from crontab ?
for example:
stdin: is not a tty line
This solution suits me ^^
I use MailTo = “myemail” at the beginning of the crontab-e line to receive all email on my mailbox
For cron which I do not wish to receive mails I add> / dev / null 2> & 1 at the end of the cron
Thank you.
I tried this, but I receive an error:
MAILTO=””
crontab: error on previous line; unexpected character found in line.
hi,
Can u please tell me if i use MAILTO=”” option to stop receiving mails from cron then
where this mails will be stored.?
Will it go in trash or in queue ?
To the best of my knowledge, cron will not even generate mail because it has no user or address to send the mail to, so it merely executed the jobs in place and the output vanishes into the great /dev/null in the sky.
MAILTO not working on my server any other solutions
Work’s like a charm.
Thanks!
It is also worth noting that you can mix this up as well. For instance, if you have ten cron jobs and you want email for eight of them, you can do as follows:
####
cron job 1
cron job 2
…
cron job 8
MAILTO=””
cron job 9
cron job 10
Cron, at least as of OS X El Capitan, which runs the latest BSD release, will use the default method of mailing the crontab owner for the first 8 jobs, then ditch any mail for the last two. You can mix it up further and specify different email addresses or user accounts for each cron job in your tab. Just note that when you use MAILTO=”foo”, cron will use that user or email address for every cron job that follows in the tab unless you redeclare MAILTO.
On some crond’s, one can handle this issue at a high level (and circumvent bad crontab files), by adding to /etc/sysconfig/crond: CRONDARGS=â€-moffâ€, and then restart the crond daemon. This tells crond not to send mail at all.
Remove the period on your first unix command. E.g. instead of
>/dev/null 2>&1.
make it just
>/dev/null 2>&1