Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
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.
|
|
03-18-2011, 05:53 AM
|
#1
|
LQ Newbie
Registered: Mar 2011
Posts: 2
Rep:
|
how to get the last sunday of March and October in shell script
Hi all,
Can any one please assist me to run the cron job only on last Sunday of March and last Sunday of October.
Regards,
|
|
|
03-18-2011, 06:55 AM
|
#2
|
Senior Member
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
|
Taking a look at the crontab manual page, there's an example:
Code:
To request the last Monday, etc. in a month, ask for the "5th" one. This will always match the last Monday, etc., even if there are only four Mondays in the month:
# run at 11 am on the first and last Mon, Tue, Wed of each month
0 11 1,5 * mon-wed date
So, OK, let's make that only the last Sunday (change 1,5 to 5, the mon-wed to sun and change the asterisk (that's for all months) to 3,10 for March and October. Then pick the time of day you want to run and change the 0 11 to the appropriate minute and hour (remember the hours are 0 - 23).
That ought to look something like this (to run at 0330):
Code:
30 3 5 3,10 sun date
You'd want to replace "date" with whatever you want to execute.
Hope this helps some.
|
|
|
03-18-2011, 07:18 AM
|
#3
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
@tronayne: which version of crontab are you running? I always believed that
Code:
0 11 1,5 * mon-wed command
would run every first and fifth of the month plus every Monday to Wednesday.
Last edited by colucix; 03-18-2011 at 07:21 AM.
|
|
|
03-18-2011, 07:47 AM
|
#4
|
Senior Member
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
|
Looks like dcron-4.4-x86_64-1. The example is right out of the man page.
I'm pretty sure that with the 1,5 the job will only be executed the first Monday, Tuesday and Wednesday and the last Monday, Tuesday and Wednesday of every month and 1100 hours. Could be wrong about that and, you know, could test it but, gee, it'll take while to get the results, eh?
Maybe there's a crontab expert that could weigh in (and, maybe, the man page is just flat wrong?)?
|
|
1 members found this post helpful.
|
03-18-2011, 07:58 AM
|
#5
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Since in my knowledge the day of month field is a cardinal and not an ordinal, I suggest another option. We have to pay attention to the fact that the day of week and the day of month fields in a crontab entry don't act together to select the day. For example:
Code:
0 9 25-31 3 Sun command
will not run only on the Sunday between 25th and 31st of March (that would be the last Sunday) but will run every Sunday of the month plus every day between 25th and 31st.
Said that, you have to check if a job scheduled on Sunday is being run on 25th to 31st of the month or if a job scheduled to run all days between 25th and 31st is being run on Sunday. In the last case:
Code:
0 9 25-31 3,10 * [[ $(date +%a) == Sun ]] && command
In addition, if the problem is to run some script at the Daylight Saving Time changes, take in mind that you can retrieve the exact days in which this change occur using the zdump command. Example:
Code:
$ /usr/sbin/zdump -v Europe/Rome | grep 2011
Europe/Rome Sun Mar 27 00:59:59 2011 UTC = Sun Mar 27 01:59:59 2011 CET isdst=0 gmtoff=3600
Europe/Rome Sun Mar 27 01:00:00 2011 UTC = Sun Mar 27 03:00:00 2011 CEST isdst=1 gmtoff=7200
Europe/Rome Sun Oct 30 00:59:59 2011 UTC = Sun Oct 30 02:59:59 2011 CEST isdst=1 gmtoff=7200
Europe/Rome Sun Oct 30 01:00:00 2011 UTC = Sun Oct 30 02:00:00 2011 CET isdst=0 gmtoff=3600
you can extract and use this information to automatically set-up your cron job every year.
|
|
|
03-18-2011, 08:02 AM
|
#6
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by tronayne
Looks like dcron-4.4-x86_64-1. The example is right out of the man page.
I'm pretty sure that with the 1,5 the job will only be executed the first Monday, Tuesday and Wednesday and the last Monday, Tuesday and Wednesday of every month and 1100 hours. Could be wrong about that and, you know, could test it but, gee, it'll take while to get the results, eh?
|
I did the same test using vixie-cron-4.1 and a job like:
Code:
30 13 1,5 3 Fri date
did run even if today is not the first or the last Friday of the Month. Most likely there are differences between Vixie cron and dcron. I will investigate further and post the results.
|
|
|
03-18-2011, 08:21 AM
|
#7
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Ok. I checked and the Dillon's cron (dcron) has this additional feature: if you specify both a day in the month and a day of week, it will be interpreted as the Nth such day in the month. Whereas Vixie cron simply states: the day of a command’s execution can be specified by two fields — day of month, and day of week. If both fields are restricted (ie, aren’t *), the command will be run when either field matches the current time.
tronayne, thank you for having brought it to light. Let's say that my solution works for every crontab flavour, but if you have dcron up and running you can take advantage of the additional feature!
|
|
|
03-18-2011, 09:56 AM
|
#8
|
Senior Member
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
|
Good to know, thanks.
BTW, the test should have run today (this is the last Friday in March, methinks).
Anyway, all is well that ends.
[EDIT]
Argghh! Daylight Savings Time (aka Stupid Time Where I Live) always messes with my head.
Apologies.
[/EDIT]
Last edited by tronayne; 03-18-2011 at 02:32 PM.
|
|
|
03-18-2011, 10:01 AM
|
#9
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by tronayne
BTW, the test should have run today (this is the last Friday in March, methinks).
|
Nope, the last one will be March, 25th. You've just earned an extra week of life!
|
|
|
03-18-2011, 11:29 AM
|
#10
|
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
|
Moved: This thread is more suitable in Linux - Software and has been moved accordingly to help your thread/question get the exposure it deserves.
|
|
|
03-18-2011, 11:59 AM
|
#11
|
LQ Newbie
Registered: Mar 2011
Posts: 2
Original Poster
Rep:
|
Hi all,
Please assist me how to run the cron job at 02:00 AM on every year Last Sunday of march.
Thanks for the above feedback
|
|
|
03-18-2011, 12:10 PM
|
#12
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by dpk
Hi all,
Please assist me how to run the cron job at 02:00 AM on every year Last Sunday of march.
Thanks for the above feedback
|
Well, given the feedback above, you should be able to write it down by yourself. What is not clear about the above discussion? Anyway, you might start to tell us which version of cron you're running. If you have dcron the solution suggested by tronayne in post #2 is well suitable. For any other cron flavour the solution suggested by me in post #5 should be what you're looking for.
|
|
|
All times are GMT -5. The time now is 06:42 PM.
|
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
|
|