Run cron job alternate week day

In this post we will learn,Run cron job alternate week day.
Today, I met with very good question i.e how to run cron job alternate week day. The user wants to run the script in alternate Saturday.When I read this question first time,it make me to take a pause for a while. Later thinking on some more logic,I ended up with nice way to accomplish this task .

What is cron

Cron is a software utility,basically a job scheduler in Linux and Unix like operating system.
Cron information are saved in crontab which is also know as cron table.The format of writing crontab is given below

Minute Hour date-Of-Month Month Day-Of-Week /path/of/command-script

Task : We got a task to run cron job alternate week day. For eg. run a cronjob in alternate Monday,Tuesday,thursday etc.

See given below examples

Time to run script : 12.30 AM
Month : Run the script every month
Day of Week : Alternate day of week

Day of week represented in Crontab as follows

Sunday = 0
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Firday = 5
Saturday = 6

On alternate Monday

30 00 1-2,15-16,29-31 * 1 give-path-of-script-command

On alternate Tuesday

30 00 1-3,15-17,29-31 * 2 give-path-of-script-command

On alternate Wednesday

30 00 1-4,15-18,29-31 * 3 give-path-of-script-command

On alternate Thursday

30 00 1-5,15-19,29-31 * 4 give-path-of-script-command

On alternate Friday

30 00 1-6,15-20,29-31 * 5 give-path-of-script-command

On alternate Saturday

30 00 1-7,15-20,29-31 * 6 give-path-of-script-command

On alternate Sunday

30 00 1-7,15-20,29-31 * 0 give-path-of-script-command

2 thoughts on “Run cron job alternate week day”

  1. I feel the above expression will not work if alternate Thursday will occur on 6-14, 20-28 For example in January 2022 Thursdays will come 6th, 13th, 20th and 27th. The other issue will come for example if alternate Thursday comes between 29-31 and next Thursday comes between 1-5 job will run for 2 consecutive Thursday.

    I had similar requirement and used an approach mentioned in this link https://stackoverflow.com/questions/68472608/conditional-cron-job-expression-for-running-task-fortnightly-and-daily

    Reply
  2. Are the entries for alternate Saturday and alternate Sunday correct ? Should they be 30 00 1-7,15-21,29-31 * 6 give-path-of-script-command .. & 30 00 1-7,15-22,29-31 * 6 give-path-of-script-command respecitvely?

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.