Linux - NewbieThis 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.
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.
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
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:
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:
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.
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.
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 ?
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.