cronic
Run cron jobs, reporting only on failures
TLDR
Call a command and display its output if it returns a non-zero exit code
SYNOPSIS
cronic command [argument...]
DESCRIPTION
cronic is a utility from the moreutils package designed to wrap commands, primarily for use within cron jobs. Its core function is to execute a given command and capture its standard output (stdout) and standard error (stderr). By default, cron sends an email whenever a scheduled job produces any output to stdout or stderr, or if it exits with a non-zero status. This can lead to an inbox flooded with emails from successful jobs that produce expected output (e.g., status messages, log entries).
cronic addresses this by only forwarding the captured output (and implicitly, triggering an email from cron) if the wrapped command exits with a non-zero status (indicating failure) OR if it produces any output on stdout or stderr. If the command succeeds and produces no output, cronic suppresses the output, effectively making the cron job silent. This allows administrators to receive notifications only when something unexpected happens – either an error occurs or a job generates significant output that needs attention – greatly reducing mail clutter. It's a small, focused tool that simplifies cron job management by making success silent and failures verbose.
CAVEATS
- Output Buffering: cronic buffers all standard output and standard error until the wrapped command completes. For long-running commands that produce large amounts of output, this can consume significant memory. Additionally, real-time progress updates are not visible until the command finishes or fails.
- Exit Status Reliance: It relies solely on the exit status of the wrapped command to determine success (zero exit status) or failure (non-zero exit status). If a command produces unexpected output but still exits with a zero status, cronic will still forward the output.
- Not a Scheduler: cronic is a command wrapper, not a cron scheduler or a job management system. It must be invoked from a cron job or similar execution environment.
COMMON USAGE EXAMPLE
To run a daily backup script called backup.sh located in /usr/local/bin silently on success but with full output on failure, you would add the following line to your user's crontab (via crontab -e
):0 2 * * * /usr/bin/cronic /usr/local/bin/backup.sh
In this example, cron will execute cronic at 2:00 AM daily. If backup.sh runs successfully and produces no output, cronic will suppress everything, and cron will not send an email. If backup.sh fails (non-zero exit status) or produces any output (e.g., 'Backup completed successfully, 100 files transferred'), cronic will forward that output to cron, triggering an email notification.
DISTINCTION FROM <B>SET -E</B>
cronic is often misunderstood as a replacement for shell constructs like set -e
. While both deal with errors, their purposes differ. set -e
in a shell script causes the script to exit immediately if any command fails (returns a non-zero exit status). cronic, on the other hand, wraps a single command (or an entire script) and only concerns itself with its output and final exit status, relaying output and status after execution. It doesn't modify the internal execution flow of the wrapped command itself.
HISTORY
cronic is part of the moreutils package, a collection of useful Unix utilities that extend the functionality of core tools. It emerged as a solution to a common pain point experienced by system administrators: the overwhelming number of emails generated by cron jobs that produce output even when successful. Before cronic, administrators often resorted to redirecting output to /dev/null (e.g., >/dev/null 2>&1
) for successful jobs, which meant losing valuable error information in case of failure. cronic provides a simple, elegant mechanism to have the best of both worlds: silence for success, and full output for failures or unexpected output. Its development reflects a pragmatic approach to daily system administration challenges.