Crontab fun

TorgoX on 2003-08-10T23:42:42

Dear Log,

Crontab is a simple format; but it's surprisingly easy to make a mistake in crontab files. For example, it's surprisingly easy to type this:

4 1,12 * * 3 * thingy --hoohah whathaveyou '*.stuff' /home/thing/stuff

and not immediately see that there's the wrong number of fields there, and that the first command to be run is "*" with parameters "thingy -hoohah whathaveyou '*.stuff' /home/thing/stuff".

Yeah. Oops.

The basic problem is that crontab files are meant to be parsed by one program, cron, but cron doesn't, and can't, do much in the way of prompting for you like "You said A, but that's odd. Did you really mean B? Press Y/n:". And this is a real problem because cron is often used for scheduling Very Important Things, and anyway the whole point of cron is to be able to forget about processes, a purpose which is defeated when you have to remember "Hm, I have to remember to look first thing tomorrow to see whether the cron job I set up for 1am is actually running right."

So I figured that the best that one could do, is to make a sanity-checker for crontab files -- something that test-parses a prospective crontab file and... what? Re-emits it in some other format? I could simply add labels to the fields ("Minute: 4. Hour: 1,2", etc), but that still fails to explain some of the more arcane crontab syntax. So I figure that the most direct thing to do is to just explain it in English. That's how I thought of crontab2english.

The idea is this: Before you put a line in a real crontab file and call "crontab thatfile" on it, you run crontab2english on that file (or, if you want, just on that line). So you write this:

4 1,12 * * 3 * thingy --hoohah whathaveyou '*.stuff' /home/thing/stuff

And it looks fine to you. But, because you are a good drone who avoids disaster by entertaining the possibility that you might make mistakes, you run it past crontab2english, and it says:

Command: (line 1)
  Run: * thingy --hoohah whathaveyou '*.stuff' /home/thing/stuff
   (Do you really mean the command to start with "*"?)
  At: 4 minutes past    1am and noon of    every Wednesday

And that lets you know that while you were puzzling over the man page for thingy to figure out the difference between "--hoohah" and "--badabing", you screwed up the first few crontab fields. So now you can change it to this, or whatever you really meant:

4 1,12 * * 3 thingy --hoohah whathaveyou '*.stuff' /home/thing/stuff

For that, crontab2english says this:

Command: (line 1)
  Run: thingy --hoohah whathaveyou '*.stuff' /home/thing/stuff
  At: 4 minutes past    1am and noon of    every Wednesday

And if that's what you meant, now you can run "crontab thatfile".

And that's why you should use crontab2english. You can download it from my CPAN directory

Of course, it's in Perl.

And no, I'm not going to localize it to any languages other than English, and no I'm not going to write english2crontab. It's too complicated.


syntax highlighting

koschei on 2003-08-11T00:27:55

viewing that entry in vim, the time fields are one colour, while the command is another. The "field too far" is in the command colour. Syntax highlighting is a marvellous thing.

crontab mails

ask on 2003-08-12T04:23:02

any responsible sys admin would read the output from cron in his mail. :-)

    - ask