Crontab – Should I Use 1,15 or */15 to Run a Command Every 15 Days?

cron

As I know crontab has those fields, starting by left to right.

1   minutes 0-59
2   hours 0-23
3   days of month 0-31
4   month 1-12
5   day of the week 0-6
6   command

I want to run foo command every 15 days at 15:30
This is correct because run command 1 and 15 of the month
month has 30 days(31 some) so is run every 15 days

30 15 1,15 * * /sbin/foo -a 1> /dev/null

Is also correct this syntax?

30 15 */15 * * /sbin/foo -a 1> /dev/null

System is Slackware Linux which use Dillon Cron

Best Answer

This syntax 30 15 */15 * * is correct, but it is not doing the same with this 30 15 1,15 * *.

The latter will execute command at 1st and 15th of the month, as it has fixed comma separated values for the "day of month" field.

The / defines steps, that means */15 will execute every 15 days, starting from 1, that means: 1st, 16th (for all months) and also 31th (for any month having 31 days).


As man crontab(5) says, step values can be used in conjunction with ranges. So if you want to have the same result using the / syntax you could do: 30 15 1-15/14 * * which means 30 15 1,15 * *.

Another example, if you want to run every 15 days, but on 5th and 20th of every month: 5-20/15. Of course, for this case, it is more readable to write 5,20. But combining the range with the steps, enables defining the start-end of the ranged execution.

For Day 1,3,5,7 etc of the Month: */2

For Day 2,4,6,8 etc of the Month: 2-30/2

For Minutes(0-59) and Hours(0-23), the first valid value is 0 so this: 0 */2 * * * means at 00:00, 02:00, 04:00 etc.

Related Question