ahalbert
ahalbert

Reputation: 474

Cron - How to run a job on the first weekday after a specific day of the month?

We want to schedule a job that runs on a weekday on the 12th or on the first weekday after the 12th. Can I specify that in cron?

Upvotes: 1

Views: 589

Answers (1)

Romeo Ninov
Romeo Ninov

Reputation: 7225

You can't define such logic in cron and the usual way is to add it in to the script you want to run. So run the script every day like this:

0 0 * * * /path/to/script.sh

and add inside

if [ "$(date +%d%m)" == "0101" ]
then rm -f /var/run/flag
fi
if [ $(date +%d) -ge 12 ] && [ $(date +%w) -gt 0 ] &&  [ $(date +%w) -lt 6 ] && [ ! -f /var/run/flag ]
then <do the work>
touch /var/run/flag
fi
exit

Also you should take care to run only once (as far as I understand)

Upvotes: 2

Related Questions