Operating System - HP-UX
1832648 Members
3017 Online
110043 Solutions
New Discussion

Disabling the message generation out of cronjobs

 
SOLVED
Go to solution
sarma
Advisor

Disabling the message generation out of cronjobs

HI
whenever I run a cronjob, it generates an email and sends it to inform it has been run. Is there a way to disable the notification when a cronjob runs based on some conditions
7 REPLIES 7
Pete Randall
Outstanding Contributor
Solution

Re: Disabling the message generation out of cronjobs

At the end of your cron entry, add "1>/dev/null 2>&1" (without the quotes).


Pete

Pete
Alan Meyer_4
Respected Contributor

Re: Disabling the message generation out of cronjobs

The most common reason for email generated by cron is the handling of stdout & stderr. It's wise to redirect the output of a cron command to log files or at the very least to /dev/null as in

> /dev/null 2>&1
" I may not be certified, but I am certifiable... "
Pete Randall
Outstanding Contributor

Re: Disabling the message generation out of cronjobs

Oops, extraneous 1 in there - do it like Alan wrote it!


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: Disabling the message generation out of cronjobs

Hi:

Any output (STDOUT or STDERR) generated by a crontask that is not redirected to a file will be emailed to the user that submitted the job. This can be quite useful as an exception reporting mechanism, especially for STDERR when your scripts are "properly" written.

In any case, if you do not want the un-redirected output, do something like:

0 1 * * * /path/scrript > /dev/null #...discard STDOUT

0 1 * * * /path/script > /dev/null 2>&1 #...discard STDOUT + STDERR

Another way to circumvent the problem is to write your scripts to determine whether or not they have a controlling terminal. Since 'cron' jobs don't, you can begin your script with:

[ -t 0 ] || exec > /dev/null 2>&1

Regards!

...JRF...

Regards!

...JRF...
Ivan Ferreira
Honored Contributor

Re: Disabling the message generation out of cronjobs

You received a mail not because the job was run, but because the job had output and could not be displayed. That's why you need to redirect stdout and stderr in the scripts. But instead of discard the output, can be recommended to redirect the output to a logfile, if the job cannot run for some reason, you can check the log file.

Like

0 * * * * /usr/local/bin/myscript.sh > /tmp/myscript.log 2>&1
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Geoff Wild
Honored Contributor

Re: Disabling the message generation out of cronjobs

Like Ivan, I tend to send output of cron to a log file - unless I don't need the info.

Examples:

0 5 * * 1 [ -d /var/adm/lp/XEBEC ] && /usr/local/bin/print.clean.receive >/dev/null 2>&1
0 6 21 * * [ -d /var/adm/lp/XEBEC ] && /usr/local/bin/lpqpurge >/tmp/lpqpurge.cronlog 2>&1

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
sarma
Advisor

Re: Disabling the message generation out of cronjobs

thank you for the information