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, |
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: That ought to look something like this (to run at 0330): Code:
30 3 5 3,10 sun date Hope this helps some. |
@tronayne: which version of crontab are you running? I always believed that
Code:
0 11 1,5 * mon-wed command |
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 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 Code:
$ /usr/sbin/zdump -v Europe/Rome | grep 2011 |
Quote:
Code:
30 13 1,5 3 Fri date |
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! :jawa: |
Good to know, thanks.
BTW, the test Quote:
Anyway, all is well that ends. [EDIT] Argghh! Daylight Savings Time (aka Stupid Time Where I Live) always messes with my head. Apologies. [/EDIT] |
Quote:
|
Moved: This thread is more suitable in Linux - Software and has been moved accordingly to help your thread/question get the exposure it deserves.
|
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 |
Quote:
|
All times are GMT -5. The time now is 07:03 PM. |