- Published on
Better Cron Emails
- Authors
- Name
- Jason R. Stevens, CFA
- @thinkjrs
In assuming you run cron jobs all the time, on all your systems, like I do, you've probably set the crontab options or the MAILTO variable within /etc/crontab configuration file or utilized stderr and stdout redirects.
There is a better way!
Meet Cronic, a shell script by Chuck Houpt
to help control the most annoying feature of cron: unwanted emailed output, or 'cram' (cron spam).
Basic Usage
Instead of writing:
* * * * * myscript >/dev/null 2>&1
you can now write:
* * * * * cronic myscript
You can avoid sweeping all cron errors under the rug, as we do with stderr and stdout redirection to the /dev/null black hole while simultaneously cleaning up the readability of your scripts.
Get it on Github
The Cronic script is available on Github via the community here: justincase/cronic. That version is v2 and I've submitted a PR to upgrade things to v3.
If you need v3 now, fork my copy at thinkjrs/cronic.
Installation
If you're a familiar 'nix user just make the script executable and put it somewhere in your path.
'Nix Scripting Step-by-step
For those readers less familiar with 'nix scripting, let's break down step by step how to install Cronic.
Open up your terminal:
- Clone the repo via
git clone git@github.com:thinkjrs/cronic. - Change directories into the new repo directory
cd cronic. - Make
cronicexecutable viachmod +x cronic. - Make a
bindirectory in your user$HOMEdirectory, i.e.mkdir $HOME/binormkdir ~/bin.Don't worry, if you have a
bindirectory within your home directory already this will not overwrite it. - Make sure that
~/binis added to your path via.profile,.bash_profile,.zsh_profile,.bashrc, or whatever shell configuration files you use.
For example, on some systems I have the following in my .bashrc:
PATH="$HOME/.local/bin:$HOME/bin:$PATH"
... # more stuff added to the PATH
export PATH # now we export it to be used by the shell
- Soft-link your executable cronic script to your
~/bindirectory. Assuming you're still in your cronic directory from Github (if not, navigate your shell there):
CRONIC_LOC=$(pwd)/cronic # set a temporary shell variable
pushd ~/bin # temporarily go into your ~/bin directory
ln -s $CRONIC_LOC cronic # create a soft-link _to_ the CRONIC_LOC named cronic
popd # return to your original cronic directory from Github
'Nix Extras
You might have noticed the use of popd and pushd above, in addition to creating some ~/bin directory for your user-space cronic script.
These are intentional as built-in programs and/or conventions commonly used in the GNU/Linux community.
- Read more on the Linux Filesystem Hierarchy.
- Dig into
pushdandpopd
If you're interested in improving overall 'nix skills check out Linux Journey.
I hope you found this useful and thanks for reading!