cron email error only
Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about cron mail only on error hiring developers or posting ads with us Server Fault Questions Tags Users Badges Unanswered Ask Question
Cron Send Email On Error_ Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute: Sign up Here's cron error failed to open pam security session success how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Cron: Only get errors in emails? up vote 22 down vote favorite 5 I finally set up a cron error bad minute realistic backup schedule on my data through a shell script, which are handled by cron on tight intervals. Unfortunately, I keep getting empty emails each time the CRON has been executed and not only when things go wrong. Is it possible to only make CRON send emails when something goes wrong, ie. my TAR doesn't execute as intended? Here's how my crontab is setup for the moment; 0 */2 * * * /bin/backup.sh 2>&1 | mail -s "Backup status"
Cron Error Can't Switch User Contextemail@example.com Thanks a lot! bash shell cron schedule share|improve this question asked Jan 24 '11 at 9:40 Industrial 53921535 add a comment| 4 Answers 4 active oldest votes up vote 32 down vote accepted Ideally you'd want your backup script to output nothing if everything goes as expected and only produce output when something goes wrong. Then use the MAILTO environment variable to send any output generated by your script to your email address. MAILTOemail@example.com 0 */2 * * * /bin/backup.sh If your script normally produces output but you don't care about it in cron, just sent it to /dev/null and it'll email you only when something is written to stderr. MAILTOfirstname.lastname@example.org 0 */2 * * * /bin/backup.sh > /dev/null share|improve this answer answered Jan 24 '11 at 9:53 Cakemox 13.1k52957 add a comment| up vote 8 down vote Using cronic wrapper script looks like a good idea; to use it you don't have to change your scripts. Instead of: 0 1 * * * backup >/dev/null 2>&1 do: 0 1 * * * cronic backup More info on http://habilis.net/cronic/. share|improve this answer edited Sep 8 at 11:39 Law29 2,3731319 answered Feb 13 '12 at 21:43 Ricardo Pardini 41638 I really don't see how that will help when the problem is nothing more than an incorrect cron line and cron is doing exactly what it is told to do. –John Gardeniers Feb 14 '12 a
output only when error occurs 7 posts / 0 new Log in or register to post comments Last post #1 Tue, 08/14/2012 - 10:48 eddieb Cron email output only when error occurs in CentOS 6.3, I have "/sbin/service iptables save" in cron. cron error cannot set security context How do I set cron to only email when there is an error or, cron error can't fork if possible, run "service" silently (unless there is an error)? If none of these options are possible, is running "iptables-save >
Cron Error Bad Username/etc/sysconfig/iptables" instead the only remaining choice? Thanks Log in or register to post comments #2 Tue, 08/14/2012 - 11:04 andreychek Howdy, Cron will email you with any output produced by the programs it's running. So the http://serverfault.com/questions/226074/cron-only-get-errors-in-emails key would be to make sure the program you're running isn't outputting anything unless it's an error. You can accomplish that by redirecting the standard output... something like this: /sbin/service iptables save >/dev/null With that, all normal output will be redirected to /dev/null, but warning/error output will still be generated (and thus emailed). -Eric Log in or register to post comments #3 Tue, 08/14/2012 - 11:31 (Reply to #2) eddieb for future https://www.virtualmin.com/node/23080 reference, I think you meant >/dev/null 2>&1 thanks! Log in or register to post comments #4 Tue, 08/14/2012 - 12:05 andreychek Howdy, Well, the forum mangled what I typed, but I meant just: >/dev/null What you mentioned there, " >/dev/null 2>&1", will cause all output, both regular and errors, to go to /dev/null... and it didn't sound like that's what you wanted. By using just ">/dev/null", only standard output is redirected to /dev/null, and errors will still be output, which will cause them to be emailed to you. -Eric Log in or register to post comments #5 Tue, 08/14/2012 - 12:13 (Reply to #4) eddieb correct, thanks! Log in or register to post comments #6 Thu, 05/15/2014 - 23:48 (Reply to #5) davehprohoods This doesn't work for me on CentOS 6.3 and 6.4; not sure why. Please let me know if you did anything additional to get it to work. Log in or register to post comments #7 Wed, 08/15/2012 - 06:44 Locutus Please note though that this method of course only works if the script/application actually does send regular output to stdout and warnings/errors to stderr. :) Of course, a sane script should do that. Log in or register to post comments
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies http://unix.stackexchange.com/questions/57594/have-cron-email-output-to-mailto-based-on-exit-status of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute: Sign up on error Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Have cron email output to MAILTO based on exit status up vote 8 down vote favorite 1 I have a cron job running a php command like this: php /path/to/script.php > dev/null This should send only STDERR output to the MAILTO cron error bad address. From what I gather the php script is not outputting any STDERR information even when its exit status is 1. How can I get the output of the php command (STDOUT) and only send it to MAILTO if the exit status is non-zero? bash cron share|improve this question asked Dec 4 '12 at 23:51 Dave 4613 add a comment| 2 Answers 2 active oldest votes up vote 10 down vote accepted php /path/to/script.php > logfile || cat logfile; rm logfile which dumps standard output into logfile and only outputs it if the script fails (exits non-zero). Note: if your script might also output to stderr then you should redirect stderr to stdout. Otherwise anything printed to stderr will cause cron to send an email even if the exit code is 0: php /path/to/script.php > logfile 2>&1 || cat logfile; rm logfile share|improve this answer edited Jan 3 '15 at 4:03 HalosGhost 3,02261733 answered Dec 5 '12 at 0:04 Kyle Jones 9,17011637 Simple and just what I needed. Thanks! –Dave Dec 5 '12 at 0:38 This will also kick out for anything appearing in stderr, which