Cron

The crontab file has 6 space separated values, the first 5 control when and the 6th is what. However the 5 when fields can be tricky to understand and also easy to forget which does what.

The way the times work is like this, where a number is specified then that is used, you can also do a comma separated list of number or use a hyphen to get a range or an asterisk to get all of them. Let's consider the "hour" field. If this is * that means "run every hour", if this is "18" then only run this during the 18th hour of the day or in other words between 18:00 and 18:59 inclusive, then "6,18" means 6am and 6pm, then finally "9-17" means 9am to 5pm inclusive.

  • minutes: in the range 0 to 59
  • hours: in the range 0 to 23
  • days of month: in the range 1 to 31
  • months: in the range 1 to 12
  • days of week: in the range 0 to 6, where 0 = Sunday

You will see it is all too easy to get the wrong outcome, for example "* 23 * * *" in theory means run at 11pm every evening, but it will actually run 60 times, because it is every minute during the 23rd hour, hence "0 23 * * *" is probably what was intended, this means once per day at 23:00 running every day. Now that you can see how "once a day" works then it should be easy to see that "0 1 * * 1-5" means "run at 1am Monday to Friday" and that "0 6 * * 0,6" means "run at 6am on the weekend". Simple once you get the hang of it but unless you do this all the time, you will probably forget, like I keeping doing, what each field means.

It is worth noting that any errors or console output from the cronjobs will go to the user's local mail. Hence it is worth redirecting all output to a file.

Have a look at Linux Shell Scripts and the section on dates to see how to log to a different file each day or execution.