The crontab system is an embodiment of several facets of the “Unix Way”. One of the famous rules of this philosophy is the “Rule of Silence”, meaning that unless something alarming or unexpected has happened, it should remain silent. By design, the scripts you are running in your crontab should produce no output, they are simply meant to run at the assigned time and do a job.
Sadly, our software is not perfect and our scripts are fallible. Servers go offline, software updates change syntax, our old assumptions may not always be valid. So we need to find some sort of happy medium between running invisibly and constantly being in our face.
We are starting with a very basic backup routine. Data on our file server is stored in /storage and we want to clone it nightly to a removable USB drive mounted at /backups. This is accomplished by the following script.
We run this script nightly from crontab as follows.
So this works pretty well, our backup job executes and since the v switch is being used we exploit a side effect of a crontab – any output is emailed to the cron job owner. We end up with a nice output of the backup job, if everything runs well we see a verbose list of files synced, if something goes wrong we see that output as well.
This couldn’t be simpler.
But there is a problem. After a few days of smooth sailing it will be very tempting to skim the daily reports and just assume that everything is going well. Then as more time passes we will simple tune them out and delete these reports. The challenge we are facing is that we still want some feed back from our backup system, but not too much that we ignore it. Essentially, we want to know:
Luckily, with just a little instrumentation added to our backup script, we can use this to create an RSS feed that we can follow in our favorite feed reader to see the health of our system at a glance.
For our added instrumentation we will be exploiting another side effect of the “Unix Way”. Specifically, the return value of the command. A “well behaved” program will return an exit code of 0 meaning “everything ran as expected”. Any other code will mean “something went wrong”.
We, aren’t going to dive into the different return codes and what they mean went wrong, we just want to know if it went as expected or not.
Let’s review the changes we have made:
Using more complicated decision structures and Boolean logic tests we could create very complicated, multi-step tests, but for now let’s stick with something simple.
Now that we have our backup script added updates the jobstatus.txt the final step is to take this primitive database and use to to create an RSS feed.
We aren’t going to get too deep into the explanation of this script (If you are new to Python, please check out the excellent guide, “Learn Python The Hard Way”).
Our “database” is set in the DATALOC, the script opens this file and iterates through each line. It reads in the line and assigns the first half (before the comma) to the description and assigns the date to the second half. If you want to, feel free to create a third field (separated by another comma) for the title of the RSS item.
After the script has parsed each RSS item it generates a feed our of all of these items and writes it to FEEDLOC. This location should be in accessible by a web-server running on this server. From there it’s just a matter of your feed reader picking up this update.
Below is a sample of our new crontab, first running the backup script, then running our RSS generator:
And with a few minutes of work we can now monitor our daily scripts and jobs from a simple glance at our RSS feed. This is definitely not suited for a real-time status, but it does well for daily or weekly tasks.
There are many improvements that could be made, better checking of status codes to report the exact nature of the error if the status is not 0, more bullet-proof scripts to begin with, but this is a good start to looking at and thinking about your repeating jobs in a different way. Keeping the status brief and to the point it can easily be glanced over and is less likely to be forgotten.