Someone brought up from our Sys Admin chat at work yesterday that crontab and % are insane, the summary was:
If you want the % character in a command, as part of a cronjob:
1. You escape the %, so it becomes \%
2. echo and pipe the command you want to run (with the escaped %) into sed
3. Have sed unescape the %
4. Pipe it into the original program
Which I responded to with roughly “if you want a ‘%’ in your cron line you actually want a shell script instead”… this turns out to be a great argumentdebate as a lot of very good Sys Admins (who were online at the time) completely disagreed with me until I’d spelled out my argument in more detail.
Problems with cron
- The syntax can be quite insane if you’re expecting it to behave like shell (hint:
cron != shell
) - There’s no widely used crontab linter (I was going to leave it at “there’s no crontab linter”, but found chkcrontab while writing this, which looks like a good start but isn’t packaged for any distro I’ve checked yet)
- Badly breaking syntax in a crontab file will cause all jobs in that file to stop running (usually with no error recorded anywhere)
- Unless you’re double entering your scheduling information you’re not going to be able to pick up the absense of the job in your monitoring solution when it fails to run
- I’m stupid (yes this is a problem with cron)
All of these have led me to break cron lots of times and even more times I’ve had to try to figure out why a scheduled job isn’t running after someone else has broken it for me. Happy days.
KISS
Whenever I’m breaking something fairly critical too often for comfort, it’s time to Keep It Simple Stupid and the way I’ve tried to do that with cron is to never ever put anything complicated on a cron line.
Lets take a simple example:
1
|
|
Intuitively I’d just expect that to do what it does on the shell (echo’s back the string, which in cron would normally make it back to someone in email form), but instead the first % will start STDIN for the command, the remaining %s will get changed to new lines and you’ll end up with an echo statement that just echo’s a single new line out to cron as it’s not interested in the STDIN fed to it.
This creates a testing problem because now to test the behaviour of the cron line I need to wait for cron to run the cron line (there’s no way to immediately confirm the validity of the line).
If we instead place the behaviour we want in a script:
1 2 |
|
and call that from cron:
1
|
|
You can be reasonably confident that it’ll do exactly the same thing when cron runs it as when you test it on the terminal.
But % is ok when it’s simple
Some people tried to make the argument that a % is really ok when it’s actually really simple, e.g.:
1
|
|
happens to work the same if you copy it to a terminal because the %s are escaped in the cron line and the escaping will happen to drop off in shell as well, but what if you want a quoted % - you’re stuffed.
Back to KISS again.
Other reasons to keep cron simple
If you’re editing cron via crontab -e
it’s far too easy to wipe out your crontab file.
While this is mostly an argument for backups, if you keep your cron files simple it may not matter as much when they get nuked accidentally as now you’ve only lost scheduling information and not critical syntax :)
Summary
If I’m not 100% certain I can copy a line out of cron and run it on the terminal I think it doesn’t belong in cron.