how to get the last sunday of March and October in shell script
Linux - SoftwareThis 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.
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.
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
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.
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
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?)?
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:
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.
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.
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!
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.