LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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


Reply
  Search this Thread
Old 03-18-2011, 05:53 AM   #1
dpk
LQ Newbie
 
Registered: Mar 2011
Posts: 2

Rep: Reputation: 0
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,
 
Old 03-18-2011, 06:55 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
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.
 
Old 03-18-2011, 07:18 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
@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.
 
Old 03-18-2011, 07:47 AM   #4
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
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.
Old 03-18-2011, 07:58 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 03-18-2011, 08:02 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by tronayne View Post
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.
 
Old 03-18-2011, 08:21 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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!
 
Old 03-18-2011, 09:56 AM   #8
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Good to know, thanks.

BTW, the test
Quote:
30 13 1,5 3 Fri date
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.
 
Old 03-18-2011, 10:01 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by tronayne View Post
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!
 
Old 03-18-2011, 11:29 AM   #10
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Moved: This thread is more suitable in Linux - Software and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 03-18-2011, 11:59 AM   #11
dpk
LQ Newbie
 
Registered: Mar 2011
Posts: 2

Original Poster
Rep: Reputation: 0
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
 
Old 03-18-2011, 12:10 PM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by dpk View Post
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.
 
  


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
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 01:45 AM
[SOLVED] GCC options: (-m64) vs (-march=native) vs (-march=core2) -- Which one(s) to use? GrapefruiTgirl Linux - Software 5 09-29-2009 07:53 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:42 PM.

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
Open Source Consulting | Domain Registration