Cron Email Only On Error
Contents |
annoying feature of cron: unwanted emailed output, or "cram" (cron spam). If the Unix Haters list was still active, I would submit the rant below to gain membership. (feedback to: chuck@habilis.net) The Disease
Cron Send Email On Error
One of the best features of cron is its automatic email - it is also its cron error mail worst feature. Cron automatically emails the output of a cron job to the user. On the face of it, this sounds like a
Cron Error Failed To Open Pam Security Session Success
great idea. Cron jobs can run automatically in the background for months at a time - so getting an email when a problem occurs sounds useful. Unfortunately, cron's idea of "output" is simultaneously too broad and too narrow to cron error bad minute actually be useful. Cron considers any output to be significant - including standard output. This interacts badly with many unix commands, which often send status info to standard out. Some commands have a quiet options, but that can turn off all error output too. To make matters worse, cron ignores command result codes, meaning that errors from quiet programs are ignored. It is almost impossible to create a non-trivial cron job that is quiet enough to run cron error can't switch user context without output, but still reports all errors. Following the principle of "Worse is Better", the typical solution is to sweep it all under the carpet by redirecting all output to /dev/null, and hoping for the best: 0 1 * * * backup >/dev/null 2>&1 Now when your cron job fails, you will never know about it. Using cron to backup your files? Sorry, the cron job has been failing due to permission errors for months - all your files are gone. Could cron be fixed? Although almost all current implementation of cron are open source, cron's pathological behavior has been petrified into the Unix standards. So if it isn't broken, it isn't cron. The only solution left is a work-around. The Cure: Cronic Download: cronic v3 Cronic is a small shim shell script for wrapping cron jobs so that cron only sends email when an error has occurred. Cronic defines an error as any non-trace error output or a non-zero result code. Cronic filters Bash execution traces (or anything matching PS4) from the error output, so jobs can be run with execution tracing to aid forensic debugging. Cronic has no options, it simply executes its arguments. 0 1 * * * cronic backup With cronic, you can turn on Bash's strict error handling and debug options (exit on error, unset variable detection and execution tracing) to make sure problems are ca
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"
Cron Error Cannot Set Security Context
in cron. How do I set cron to only email when there is an cron error can't fork error or, if possible, run "service" silently (unless there is an error)? If none of these options are possible, is running
Cron Error Bad Username
"iptables-save > /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 http://habilis.net/cronic/ it's running. So the 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 https://www.virtualmin.com/node/23080 (Reply to #2) eddieb for future 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
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 http://www.fvue.nl/wiki/Cron:_mail_only_when_things_go_wrong 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 address. cron error bad 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 doesn't
this as the cron job: cronjob.sh >> logfile 2>&1 || echo "Error occurred. See $(pwd)/log for details" Explanation: Both stderr and stdout are redirected to a logfile. If cronjob.sh exits with an error, an error message is written to stdout, which cron will mail automatically to the cron owner. Specify MAILTO=username@domain if you want mail to be sent to an alternate address. See also Bash: Catch error Journal 20060529 I was thinking about redirecting only stderr to screen, and both stdout and stderr to a log file. Example file t.sh: echo stderr A >&2 echo stdout echo stderr B >&2 exit 1 From SHELLdorado - Shell Tips & Tricks (Advanced), I found this solution: ((bash t.sh 2>&1 1>&3 | tee ~/err.txt) 3>&1 1>&2 | tee ~/out.txt) > ~/mix.txt 2>&1 but in mix.txt stderr is shown after stdout: stderr A stderr B stdout I would like the output of mix.txt to be the output of bash t.sh 2>&1: stderr A stdout stderr B Advertisement Comments Please enable JavaScript to view the comments powered by Disqus. blog comments powered by Disqus Retrieved from "http://www.fvue.nl/w/index.php?title=Cron:_mail_only_when_things_go_wrong&oldid=9949" Categories: CronFixed problems Navigation menu Personal tools Log in Namespaces Page Discussion Variants Views Read View source View history More Search Navigation Main Page Recent changes Random page Advertisement Tools What links here Related changes Special pages Printable version Permanent link Page information This page was last modified on 2 February 2013, at 02:36. This page has been accessed 5,117 times. Content is available under Attribution-ShareAlike 3.0 Unported unless otherwise noted. Privacy policy About FVue Disclaimers