LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-06-2012, 10:54 AM   #1
zkab
Member
 
Registered: Dec 2007
Posts: 124

Rep: Reputation: 15
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 ?
 
Old 07-06-2012, 10:59 AM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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

Last edited by suicidaleggroll; 07-06-2012 at 11:13 AM. Reason: switched from list to range
 
Old 07-06-2012, 11:04 AM   #3
zkab
Member
 
Registered: Dec 2007
Posts: 124

Original Poster
Rep: Reputation: 15
Thanks - but what will happen if Thursday is on day 29-31 ?
 
Old 07-06-2012, 11:07 AM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Quote:
Originally Posted by zkab View Post
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).

Last edited by suicidaleggroll; 07-06-2012 at 11:08 AM.
 
Old 07-06-2012, 11:27 AM   #5
zkab
Member
 
Registered: Dec 2007
Posts: 124

Original Poster
Rep: Reputation: 15
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 ?
 
Old 07-06-2012, 11:40 AM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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

Last edited by suicidaleggroll; 07-06-2012 at 11:50 AM.
 
Old 07-07-2012, 05:26 AM   #7
zkab
Member
 
Registered: Dec 2007
Posts: 124

Original Poster
Rep: Reputation: 15
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
 
Old 07-07-2012, 07:33 AM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 14,929

Rep: Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520
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

Last edited by michaelk; 07-07-2012 at 08:05 AM.
 
Old 07-07-2012, 09:10 AM   #9
zkab
Member
 
Registered: Dec 2007
Posts: 124

Original Poster
Rep: Reputation: 15
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 ?
 
Old 07-07-2012, 11:11 AM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 14,929

Rep: Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520
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...
 
Old 07-07-2012, 12:08 PM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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
 
Old 07-08-2012, 08:00 PM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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.
 
Old 07-09-2012, 05:01 AM   #13
zkab
Member
 
Registered: Dec 2007
Posts: 124

Original Poster
Rep: Reputation: 15
OK - that makes sense
 
Old 07-09-2012, 08:04 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Yeah, also more portable
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
linux crontab vs unix crontab ytd Linux - General 2 08-09-2009 06:07 AM
crontab thecaligarmo Linux - Newbie 7 10-28-2008 01:38 AM
replaced crontab, now should get crontab back to what it was raminn Linux - Newbie 2 10-20-2008 08:15 PM
man crontab(5) vs crontab(1) Canis Polaris Linux - Newbie 2 06-04-2008 05:03 PM
system-wide crontab in /etc/crontab ner Linux - General 2 11-18-2003 01:35 PM


All times are GMT -5. The time now is 01:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration