├── cronic ├── README.md └── LICENSE /cronic: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Cronic v3 - cron job report wrapper 4 | # Copyright 2007-2016 Chuck Houpt. No rights reserved, whatsoever. 5 | # Public Domain CC0: http://creativecommons.org/publicdomain/zero/1.0/ 6 | 7 | set -eu 8 | 9 | TMP=$(mktemp -d) 10 | OUT=$TMP/cronic.out 11 | ERR=$TMP/cronic.err 12 | TRACE=$TMP/cronic.trace 13 | 14 | set +e 15 | "$@" >$OUT 2>$TRACE 16 | RESULT=$? 17 | set -e 18 | 19 | PATTERN="^${PS4:0:1}\\+${PS4:1}" 20 | if grep -aq "$PATTERN" $TRACE 21 | then 22 | ! grep -av "$PATTERN" $TRACE > $ERR 23 | else 24 | ERR=$TRACE 25 | fi 26 | 27 | if [ $RESULT -ne 0 -o -s "$ERR" ] 28 | then 29 | echo "Cronic detected failure or error output for the command:" 30 | echo "$@" 31 | echo 32 | echo "RESULT CODE: $RESULT" 33 | echo 34 | echo "ERROR OUTPUT:" 35 | cat "$ERR" 36 | echo 37 | echo "STANDARD OUTPUT:" 38 | cat "$OUT" 39 | if [ $TRACE != $ERR ] 40 | then 41 | echo 42 | echo "TRACE-ERROR OUTPUT:" 43 | cat "$TRACE" 44 | fi 45 | fi 46 | 47 | rm -rf "$TMP" 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | See http://habilis.net/cronic/. Keeping a copy safe for personal use. 2 | 3 | # Cronic 4 | 5 | ## A cure for Cron's chronic email problem 6 | 7 | The Disease: 8 | 9 | 0 1 * * * backup >/dev/null 2>&1 10 | 11 | The Cure: 12 | 13 | 0 1 * * * cronic backup 14 | 15 | Cronic is a shell script to help control the most annoying feature of 16 | cron: unwanted emailed output, or "cram" (cron spam). If the Unix Haters 17 | list was still active, I would submit the rant below to gain membership. 18 | - [Chuck Houpt](http://habilis.net/chuck/) 19 | 20 | ## The Disease 21 | 22 | One of the best features of cron is its automatic email - it is also its 23 | worst feature. Cron automatically emails the output of a cron job to the 24 | user. On the face of it, this sounds like a great idea. Cron jobs can run 25 | automatically in the background for months at a time - so getting an email 26 | when a problem occurs sounds useful. 27 | 28 | Unfortunately, cron's idea of "output" is simultaneously too broad and too 29 | narrow to actually be useful. Cron considers any output to be significant 30 | - including standard output. This interacts badly with many unix commands, 31 | which often send status info to standard out. Some commands have a quiet 32 | options, but that can turn off all error output too. To make matters worse, 33 | cron ignores command result codes, meaning that errors from quiet programs 34 | are ignored. 35 | 36 | It is almost impossible to create a non-trivial cron job that is quiet enough 37 | to run without output, but still reports all errors. Following the principle 38 | of "Worse is Better", the typical solution is to sweep it all under the carpet 39 | by redirecting all output to /dev/null, and hoping for the best: 40 | 41 | 0 1 * * * backup >/dev/null 2>&1 42 | 43 | Now when your cron job fails, you will never know about it. Using cron to backup 44 | your files? Sorry, the cron job has been failing due to permission errors for months 45 | - all your files are gone. 46 | 47 | Could cron be fixed? Although almost all current implementation of cron are open source, 48 | cron's pathological behavior has been petrified into the Unix standards. So if it isn't 49 | broken, it isn't cron. The only solution left is a work-around. 50 | 51 | ## The Cure: Cronic 52 | 53 | Download: [cronic](http://habilis.net/cronic/cronic) v3 54 | 55 | Cronic is a small shim shell script for wrapping cron jobs so that cron only sends email 56 | when an error has occurred. Cronic defines an error as any non-trace error output or a 57 | non-zero result code. Cronic filters Bash execution traces (or anything matching PS4) from 58 | the error output, so jobs can be run with execution tracing to aid forensic debugging. 59 | Cronic has no options, it simply executes its arguments. 60 | 61 | 0 1 * * * cronic backup 62 | 63 | With cronic, you can turn on Bash's strict error handling and debug options (exit on error, 64 | unset variable detection and execution tracing) to make sure problems are caught early. 65 | For example: 66 | 67 | #!/bin/bash 68 | 69 | set -o errexit -o nounset -o xtrace 70 | 71 | cp -rp data1 /backup 72 | cp -rp data2 /backup 73 | cp -rp data3 /backup 74 | 75 | When an error is detected, Cronic outputs a report listing the result code, error output, and 76 | combined trace and error output. The combined output can help put error messages in context. 77 | An example: 78 | 79 | From: user@example.net (Cron Daemon) 80 | To: user@example.net 81 | Subject: Cron cronic backup 82 | 83 | Cronic detected failure or error output for the command: 84 | backup 85 | 86 | RESULT CODE: 1 87 | 88 | ERROR OUTPUT: 89 | cp: data2: Permission denied 90 | 91 | STANDARD OUTPUT: 92 | 93 | TRACE-ERROR OUTPUT: 94 | + cp -rp data1 /backup 95 | + cp -rp data2 /backup 96 | cp: data2: Permission denied 97 | 98 | ### Version History 99 | * v3 - Use mktemp-d to avoid race-conditions and security problems. 100 | * v2 - Corrected command evaluation, so shell meta-chars are preserved correctly (Thanks to Frank Wallingford for the fix). 101 | * v1 - Initial release. 102 | 103 | ### Releated Projects 104 | 105 | Cronic isn't the only cure for cron. Cronic's main advantage it is small, simple and a shell script. There are several C-based 106 | cron wrapper programs with many additional capabilities and options: 107 | 108 | [Shush](http://web.taranis.org/shush/): a C-based cron wrapper, with multiple report formats, syslogging, etc. 109 | [Cronwrap](http://www.uow.edu.au/~sah/cronwrap.html): a C-based cron wrapper, with time-out control, logging, etc. 110 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Public Domain CC0: http://creativecommons.org/publicdomain/zero/1.0/ 2 | 3 | CC0 1.0 Universal 4 | 5 | Statement of Purpose 6 | 7 | The laws of most jurisdictions throughout the world automatically confer 8 | exclusive Copyright and Related Rights (defined below) upon the creator and 9 | subsequent owner(s) (each and all, an "owner") of an original work of 10 | authorship and/or a database (each, a "Work"). 11 | 12 | Certain owners wish to permanently relinquish those rights to a Work for the 13 | purpose of contributing to a commons of creative, cultural and scientific 14 | works ("Commons") that the public can reliably and without fear of later 15 | claims of infringement build upon, modify, incorporate in other works, reuse 16 | and redistribute as freely as possible in any form whatsoever and for any 17 | purposes, including without limitation commercial purposes. These owners may 18 | contribute to the Commons to promote the ideal of a free culture and the 19 | further production of creative, cultural and scientific works, or to gain 20 | reputation or greater distribution for their Work in part through the use and 21 | efforts of others. 22 | 23 | For these and/or other purposes and motivations, and without any expectation 24 | of additional consideration or compensation, the person associating CC0 with a 25 | Work (the "Affirmer"), to the extent that he or she is an owner of Copyright 26 | and Related Rights in the Work, voluntarily elects to apply CC0 to the Work 27 | and publicly distribute the Work under its terms, with knowledge of his or her 28 | Copyright and Related Rights in the Work and the meaning and intended legal 29 | effect of CC0 on those rights. 30 | 31 | 1. Copyright and Related Rights. A Work made available under CC0 may be 32 | protected by copyright and related or neighboring rights ("Copyright and 33 | Related Rights"). Copyright and Related Rights include, but are not limited 34 | to, the following: 35 | 36 | i. the right to reproduce, adapt, distribute, perform, display, communicate, 37 | and translate a Work; 38 | 39 | ii. moral rights retained by the original author(s) and/or performer(s); 40 | 41 | iii. publicity and privacy rights pertaining to a person's image or likeness 42 | depicted in a Work; 43 | 44 | iv. rights protecting against unfair competition in regards to a Work, 45 | subject to the limitations in paragraph 4(a), below; 46 | 47 | v. rights protecting the extraction, dissemination, use and reuse of data in 48 | a Work; 49 | 50 | vi. database rights (such as those arising under Directive 96/9/EC of the 51 | European Parliament and of the Council of 11 March 1996 on the legal 52 | protection of databases, and under any national implementation thereof, 53 | including any amended or successor version of such directive); and 54 | 55 | vii. other similar, equivalent or corresponding rights throughout the world 56 | based on applicable law or treaty, and any national implementations thereof. 57 | 58 | 2. Waiver. To the greatest extent permitted by, but not in contravention of, 59 | applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and 60 | unconditionally waives, abandons, and surrenders all of Affirmer's Copyright 61 | and Related Rights and associated claims and causes of action, whether now 62 | known or unknown (including existing as well as future claims and causes of 63 | action), in the Work (i) in all territories worldwide, (ii) for the maximum 64 | duration provided by applicable law or treaty (including future time 65 | extensions), (iii) in any current or future medium and for any number of 66 | copies, and (iv) for any purpose whatsoever, including without limitation 67 | commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes 68 | the Waiver for the benefit of each member of the public at large and to the 69 | detriment of Affirmer's heirs and successors, fully intending that such Waiver 70 | shall not be subject to revocation, rescission, cancellation, termination, or 71 | any other legal or equitable action to disrupt the quiet enjoyment of the Work 72 | by the public as contemplated by Affirmer's express Statement of Purpose. 73 | 74 | 3. Public License Fallback. Should any part of the Waiver for any reason be 75 | judged legally invalid or ineffective under applicable law, then the Waiver 76 | shall be preserved to the maximum extent permitted taking into account 77 | Affirmer's express Statement of Purpose. In addition, to the extent the Waiver 78 | is so judged Affirmer hereby grants to each affected person a royalty-free, 79 | non transferable, non sublicensable, non exclusive, irrevocable and 80 | unconditional license to exercise Affirmer's Copyright and Related Rights in 81 | the Work (i) in all territories worldwide, (ii) for the maximum duration 82 | provided by applicable law or treaty (including future time extensions), (iii) 83 | in any current or future medium and for any number of copies, and (iv) for any 84 | purpose whatsoever, including without limitation commercial, advertising or 85 | promotional purposes (the "License"). The License shall be deemed effective as 86 | of the date CC0 was applied by Affirmer to the Work. Should any part of the 87 | License for any reason be judged legally invalid or ineffective under 88 | applicable law, such partial invalidity or ineffectiveness shall not 89 | invalidate the remainder of the License, and in such case Affirmer hereby 90 | affirms that he or she will not (i) exercise any of his or her remaining 91 | Copyright and Related Rights in the Work or (ii) assert any associated claims 92 | and causes of action with respect to the Work, in either case contrary to 93 | Affirmer's express Statement of Purpose. 94 | 95 | 4. Limitations and Disclaimers. 96 | 97 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 98 | surrendered, licensed or otherwise affected by this document. 99 | 100 | b. Affirmer offers the Work as-is and makes no representations or warranties 101 | of any kind concerning the Work, express, implied, statutory or otherwise, 102 | including without limitation warranties of title, merchantability, fitness 103 | for a particular purpose, non infringement, or the absence of latent or 104 | other defects, accuracy, or the present or absence of errors, whether or not 105 | discoverable, all to the greatest extent permissible under applicable law. 106 | 107 | c. Affirmer disclaims responsibility for clearing rights of other persons 108 | that may apply to the Work or any use thereof, including without limitation 109 | any person's Copyright and Related Rights in the Work. Further, Affirmer 110 | disclaims responsibility for obtaining any necessary consents, permissions 111 | or other rights required for any use of the Work. 112 | 113 | d. Affirmer understands and acknowledges that Creative Commons is not a 114 | party to this document and has no duty or obligation with respect to this 115 | CC0 or use of the Work. 116 | 117 | For more information, please see 118 | 119 | --------------------------------------------------------------------------------