Skip to content

Run a job every first Sun of a month

I came across an interesting cron issue recently. The requirement was to run a job every _first_ Sunday at 12:00PM of each month.

After searching across various sites, skimming through cron manpages, I finally found the following one-liner

0 0 1-7 * 0 <user> <job>

Can you believe this simple solution? The reasoning is that Sun will be between 1 to 7th of each month. Once a Sun comes, the job will execute between 1st and 7th just once. After that any further Sundays will have a date greater than 7, and thus never execute!

Sometimes, the simplest solution is the most elegant. BTW, I had read about many other complicated solutions, such as having your own logic to determine the day, apple script, bash script solutions. I was about to give up, when I hit the jackpot! 😀

Leave a Reply

Your email address will not be published. Required fields are marked *

four × four =