Linux – cron job on 1st non-weekend day of the month

linux

I need to run a crontab command on the 1st of every month except if the 1st is a Saturday or a Sunday. In that case the command should be run the following Monday.Running the command on Monday 1st, 2nd or 3rd should have done the job but it doesn't seem to work. I've tried this

45 06 1-3 * 1  mycmd.ksh >cmd1.log

45 06 1 * 2-5  mycmd.ksh >cmd2.log

The second command is running everyday at 6h45 According to my understanding it should be running at 06h45 on the 1st of any month if the day is Tuesday through Friday. It ran today Friday the 21st at 6h45 !

Any clue ?

Thanks

Best Answer

As the man page for crontab(5) makes pretty clear:

Note: The day of a command's execution can be specified in the following two fields — 'day of month', and 'day of week'. If both fields are restricted (i.e., do not contain the "*" character), 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.

You will need to write a crontab entry that runs the job on the first, second, and third of every month, and modify the job to check what day it is to decide whether or not to continue.

Related Topic