GithubHelp home page GithubHelp logo

benjaminoakes / cronic Goto Github PK

View Code? Open in Web Editor NEW

This project forked from justincase/cronic

3.0 3.0 0.0 106 KB

A cure for Cron's chronic email problem

Home Page: http://habilis.net/cronic/

License: Other

Shell 100.00%

cronic's Introduction

See http://habilis.net/cronic/. Keeping a copy safe for personal use.

Cronic

A cure for Cron's chronic email problem

The Disease:

0 1 * * * backup >/dev/null 2>&1

The Cure:

0 1 * * * cronic backup

Cronic is a shell script to help control the most 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.

Installation

Place cronic in /usr/local/bin. Note that you may need to set the path in your crontab like so:

PATH="/usr/local/bin:/usr/bin:/bin"
0 1 * * * cronic backup

To verify that cronic works, you can make a simple crontab and check your email:

MAILTO="[email protected]"
PATH="/usr/local/bin:/usr/bin:/bin"
* * * * * cronic uname

You can also monitor the output in your cron log (e.g. tail -f /var/log/cron, location may vary on your system):

Mar 29 17:25:01 hostname cronic-stdout[29170]: Linux

To test that errors send emails, change to something that you expect to fail (e.g. cronic fail). The cron log should show something like this as well:

Mar 29 17:32:01 hostname cronic-stderr[29327]: /usr/local/bin/cronic: line 34: fail: command not found

The Disease

One of the best features of cron is its automatic email - it is also its worst feature. Cron automatically emails the output of a cron job to the user. On the face of it, this sounds like a 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 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 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 v2

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 caught early. For example:

#!/bin/bash

set -o errexit -o nounset -o xtrace

cp -rp data1 /backup
cp -rp data2 /backup
cp -rp data3 /backup

When an error is detected, Cronic outputs a report listing the result code, error output, and combined trace and error output. The combined output can help put error messages in context. An example:

From: [email protected] (Cron Daemon)
To: [email protected]
Subject: Cron <user@server> cronic backup

Cronic detected failure or error output for the command:
backup

RESULT CODE: 1

ERROR OUTPUT:
cp: data2: Permission denied

STANDARD OUTPUT:

TRACE-ERROR OUTPUT:
+ cp -rp data1 /backup
+ cp -rp data2 /backup
cp: data2: Permission denied

Version History

  • v2 - Corrected command evaluation, so shell meta-chars are preserved correctly (Thanks to Frank Wallingford for the fix).
  • v1 - Initial release.

Releated Projects

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 cron wrapper programs with many additional capabilities and options:

Shush: a C-based cron wrapper, with multiple report formats, syslogging, etc.
Cronwrap: a C-based cron wrapper, with time-out control, logging, etc.

cronic's People

Contributors

benjaminoakes avatar

Stargazers

 avatar Matt Vinall avatar  avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.