Tuesday, March 15, 2022

[SOLVED] How to schedule to run first Sunday of every month

Issue

I am using Bash on RedHat. I need to schedule a cron job to run at at 9:00 AM on first Sunday of every month. How can I do this?


Solution

You can put something like this in the crontab file:

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

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

If you run this script only on Sundays, it should mean that it runs only on the first Sunday of the month.

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



Answered By - Lukasz Stelmach
Answer Checked By - Robin (WPSolving Admin)