ring bearer ring bearer - 7 months ago 70
Bash Question

cron : how to schedule to run first Sunday of every month

Hi I need to schedule a cron job to run at at 9:00 AM on first Sunday of every month.
Did a little research and see that there is no short hand in cron to this.
Do you know of an optimal way ?
Using Bash+RHL

Answer

You can put something like that in crontab file:

00 09 * * 7 [ $(date +\%d) -le 07 ] && /run/your/script

The date +%d gives you number of current day and than you can check if the day is lesser or equal of 7. If it is that run your command.

If you run these script only in Sundays it should means that it runs only in the first Sunday of the month.

Remember, that in the crontab file the formatting options for date command should be escaped.