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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
07-06-2012, 10:54 AM
|
#1
|
Member
Registered: Dec 2007
Posts: 144
Rep:
|
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 ?
|
|
|
07-06-2012, 10:59 AM
|
#2
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
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
|
|
|
07-06-2012, 11:04 AM
|
#3
|
Member
Registered: Dec 2007
Posts: 144
Original Poster
Rep:
|
Thanks - but what will happen if Thursday is on day 29-31 ?
|
|
|
07-06-2012, 11:07 AM
|
#4
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
Quote:
Originally Posted by zkab
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.
|
|
|
07-06-2012, 11:27 AM
|
#5
|
Member
Registered: Dec 2007
Posts: 144
Original Poster
Rep:
|
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 ?
|
|
|
07-06-2012, 11:40 AM
|
#6
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
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.
|
|
|
07-07-2012, 05:26 AM
|
#7
|
Member
Registered: Dec 2007
Posts: 144
Original Poster
Rep:
|
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
|
|
|
07-07-2012, 07:33 AM
|
#8
|
Moderator
Registered: Aug 2002
Posts: 26,267
|
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.
|
|
|
07-07-2012, 09:10 AM
|
#9
|
Member
Registered: Dec 2007
Posts: 144
Original Poster
Rep:
|
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 ?
|
|
|
07-07-2012, 11:11 AM
|
#10
|
Moderator
Registered: Aug 2002
Posts: 26,267
|
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...
|
|
|
07-07-2012, 12:08 PM
|
#11
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
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
|
|
|
07-08-2012, 08:00 PM
|
#12
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,406
|
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.
|
|
|
07-09-2012, 05:01 AM
|
#13
|
Member
Registered: Dec 2007
Posts: 144
Original Poster
Rep:
|
OK - that makes sense
|
|
|
07-09-2012, 08:04 PM
|
#14
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,406
|
Yeah, also more portable
|
|
|
All times are GMT -5. The time now is 01:17 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|