Skip to content
Advertisement

crontab (cronie), run certain days of the month

I want to make a crontab entry which will run on every weekday except the 1st, 2nd, and 3rd of each month. Here’s what I have:

45   8,12,16 4-31 * 1-5  my_program

I thought that this had the following meaning:

minutes:      45 past the hour
hours:        8, 12, and 16
day of month: 4th to 31st inclusive
month:        all
day of week:  Monday to Friday inclusive

That is what I read in the crontab(5) manual page. However, I can see that the job ran at 08:45 today, Monday 2nd October. I am certain the crontab file was loaded correctly (and crontab -l shows it); it wasn’t changed recently. The system date and time as shown by date is also correct.

What am I missing to make sure my cron job only runs from the 4th onwards? I am using Fedora Linux:

% rpm -qf `which crontab`
cronie-1.5.0-3.fc23.x86_64
% rpm -q fedora-release
fedora-release-23-1.noarch

Advertisement

Answer

From crontab manual:

Note: The day of a command’s execution can be specified by two fields — day of month, and day of week. If both fields are restricted (i.e., aren’t *), the command will be run when either field matches the current time. For example, “30 4 1,15 * 5” would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday. One can, however, achieve the desired result by adding a test to the command (see the last example in EXAMPLE CRON FILE below).

The mentioned example looks like this:

0 4 8-14 * *    test $(date +%u) -eq 6 && echo "2nd Saturday"

So in your case it would be like this (not tested):

45   8,12,16 4-31 * *  test $(date +%u) -lt 6 && my_program
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement