5

I have 5 shell scripts, I want to run them in 5 days cycle, like follows:

Day 1 : script 1 Day 2 : script 2 Day 3 : script 3 Day 4 : script 4 Day 5 : script 5

Day 6 : script 1 Day 7 : script 2 Day 8 : script 3 Day 9 : script 4 Day 10 : script 5

And keep the scripts runing in 5 day cycle. How can I set it in cron jobs?

Thanks

8

cron by itself doesn't support this. The eariest way to accomplish what you want is probably to ask cron to execute a dispatcher script every day (at the same time) and have the dispatcher script decide which other script to run based on what day it is. For example:

#!/bin/sh

case $(expr $(date +%s) / 86400 % 5) in
   0)
       exec /script/for/day/1
       ;;
   1)
       exec /script/for/day/2
       ;;
   2)
       exec /script/for/day/3
       ;;
   3)
       exec /script/for/day/4
       ;;
   4)
       exec /script/for/day/5
       ;;
esac
  • +1, I like the expr trick to get the day since epoch. – Rmano Feb 18 '15 at 9:25
  • Arithmetic expansion is in all POSIX shells: $(($(date +%s) / 86400 % 5)). Beware that depending on the time at which this command runs and the DST rules in your timezeone, I think there's a possibility that you'll skip or repeat a day on a DST changeover. – Gilles Feb 18 '15 at 22:43
  • @Gilles That's always a possibility if you schedule cron jobs between 1:00 and 2:00 local time in a timezone with DST (once a year those times don't exist, and once a year they occur twice), so just avoid those times, or campaign for the abolishment of DST (I wish you success!). This trick neither improves or worsens that because it's based on the UTC day. – Celada Feb 19 '15 at 0:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.