LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   crontab help (https://www.linuxquestions.org/questions/linux-newbie-8/crontab-help-4175415248/)

zkab 07-06-2012 09:54 AM

crontab help
 
I want to run 4 scripts (scr1-scr4) in following way:

scr1: every first Thursday 02:30 every month
scr2: every second Thursday 02:30 every month
scr3: every third Thursday 02:30 every month
scr4: every fourth Thursday 02:30 every month

How will the crontab look like ?

suicidaleggroll 07-06-2012 09:59 AM

Maybe this would work?

Code:

30 2 1-7 * 4 scr1
30 2 8-14 * 4 scr2
30 2 15-21 * 4 scr3
30 2 22-28 * 4 scr4


zkab 07-06-2012 10:04 AM

Thanks - but what will happen if Thursday is on day 29-31 ?

suicidaleggroll 07-06-2012 10:07 AM

Quote:

Originally Posted by zkab (Post 4720873)
Thanks - but what will happen if Thursday is on day 29-31 ?

Then that will be the 5th Thursday in the month, you didn't say what to do on the 5th Thursday (for those months that have one).

zkab 07-06-2012 10:27 AM

Yes - your are right - my mistake.
What I want to do is run scr1 on the first Thursday of every month, scr2 on the following Thursday ... and so on
As an example, next month in August Thursdays occur on:

2 -> scr1
9 -> scr2
16 -> scr3
23 -> scr4
30 -> scr1

Is there a way to get it done with date function ?

suicidaleggroll 07-06-2012 10:40 AM

So what would happen in your example in September?

Would it run like this:
Aug 2 -> scr1
Aug 9 -> scr2
Aug 16 -> scr3
Aug 23 -> scr4
Aug 30 -> scr1
Sep 6 -> scr1

Or would you go to scr2 on Sep 6 since scr1 already ran on Aug 30?



If you want to restart with scr1 on the first Thurs of every month, regardless of which code you ended on in the previous month, I would just add a 5th entry to the crontab:
Code:

30 2 1-7 * 4 /path/to/scr1
30 2 8-14 * 4 /path/to/scr2
30 2 15-21 * 4 /path/to/scr3
30 2 22-28 * 4 /path/to/scr4
30 2 29-31 * 4 /path/to/scr1


If you want to keep the 1,2,3,4,1,2,3,4 pattern going (meaning scr1 will not run on the first thurs of every month, it will steadily move to the last thurs of the month, then the fourth, then the third, second, first, and back to the last), then I would probably set up one crontab entry to do something like this:
Code:

30 2 * * 4 /path/to/scrmaster
Then scrmaster would be a wrapper that calls scr1 on the first call, scr2 on the second call, then scr3, then scr4, then restarts from the beginning.
It could look something like this:
Code:

#!/bin/bash

if [[ -e ~/.lastscr ]]; then
  last=$(cat ~/.lastscr)
else
  last=4
fi

case $last in
1)
  /path/to/scr2
  echo 2 > ~/.lastscr
;;
2)
  /path/to/scr3
  echo 3 > ~/.lastscr
;;
3)
  /path/to/scr4
  echo 4 > ~/.lastscr
;;
*)
  /path/to/scr1
  echo 1 > ~/.lastscr
;;
esac

exit 0


zkab 07-07-2012 04:26 AM

Thanks for your input - I will go for:

30 2 1-7 * 4 /path/to/scr1
30 2 8-14 * 4 /path/to/scr2
30 2 15-21 * 4 /path/to/scr3
30 2 22-28 * 4 /path/to/scr4
30 2 29-31 * 4 /path/to/scr1

michaelk 07-07-2012 06:33 AM

FYI quoted from the cron wiki

Quote:

While normally the job is executed when the time/date specification fields all match the current time and date, there is one exception: if both "day of month" and "day of week" are restricted (not "*"), then either the "day of month" field (3) or the "day of week" field (5) must match the current day.
Which means your script will run if the day is 1-7 or if the day is Thurs and not just once. One of our members just posted that there are special characters to specify particular days of the month if you version supports it.

http://en.wikipedia.org/wiki/Cron

30 2 1-7 * * [ `date +%u` = 4 ] && /path/to/scr1

zkab 07-07-2012 08:10 AM

Don't understand how this works ... in the example you gave 30 2 1-7 * * [ `date +%u` = 4 ] && /path/to/scr1 both "day of month" and "day of week" are not-restricted ('*') so the exception is not valid here ... or ?

michaelk 07-07-2012 10:11 AM

Correct.

Here is another way for running a script on the 1st Thursday of the month:
Quote:

30 2 * * 4 [ `date '+%e'` -le 7 ] && /path/to/scr1
In my other post cron will run from days 1 to 7 but the script will only execute if the day is Thurs. In the above example, cron will run every Thurs but the script will only execute if the day < 7.

As stated the problem with suicidaleggroll's suggestion is that your script will run on days 1-7 and also runs on Thurs.

Other examples can be found by googling...

suicidaleggroll 07-07-2012 11:08 AM

I didn't realize that the day of month and day of week fields behaved as an "or" rather than an "and". Thanks for the clarification.

In that case my earlier suggestion will not work, you'll have to go with one of michaelk's

chrism01 07-08-2012 07:00 PM

Traditionally, you'd write a master script that ran every Thursday, and then in the top of that script do any fine calcs as to which fn/sub-script to action.
Just an alternative to think about.

zkab 07-09-2012 04:01 AM

OK - that makes sense

chrism01 07-09-2012 07:04 PM

Yeah, also more portable :)


All times are GMT -5. The time now is 04:50 PM.