LinuxCommandLibrary

cronic

Run cron jobs, reporting only on failures

TLDR

Call a command and display its output if it returns a non-zero exit code

$ cronic [command]
copy

SYNOPSIS

cronic command [args…]

DESCRIPTION

Cronic is a simple shell script designed to wrap commands executed by cron, suppressing unnecessary email notifications for transient failures while ensuring real errors are reported.

It executes the given command, capturing its stdout and stderr separately, along with the exit status. Cronic considers a command to have failed only if two conditions are met simultaneously: the command exits with a non-zero status and produces any output on stderr. In such cases, it outputs the stderr to the cron mailer, triggering an email. Otherwise, no output is sent, reducing noise from minor issues like network timeouts or empty results.

This behavior addresses a common pain point in cron jobs, where even successful commands with warnings or stderr noise trigger emails. By requiring both failure indicators, cronic provides cleaner logs and alerts only for genuine problems.

Installation is straightforward: download the script, make it executable, and place it in your PATH (e.g., /usr/local/bin/cronic). Usage in crontab is simple, e.g., 0 * * * * cronic /path/to/script.sh. It preserves the original command's stdout for logging if needed, but focuses on error reporting.

CAVEATS

Cronic is a shell script relying on bash features; ensure compatibility. It does not alter command environment variables unless passed. Non-zero exit alone (without stderr) is ignored as success.

EXIT STATUS

Exits with the wrapped command's status if failed (non-zero + stderr); otherwise 0.

EXAMPLE

crontab -e
0 2 * * * cronic myscript.sh arg1
Only emails if myscript.sh exits !=0 and has stderr output.

HISTORY

Created by Dennis Schubert in 2005 (initial release around 2006) to solve cron email spam. Hosted at hcs.harvard.edu/~dsk, it remains popular for its simplicity and has inspired forks like chronic.

SEE ALSO

cron(8), chronic(1), timeout(1)

Copied to clipboard