LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   how to get the last sunday of March and October in shell script (https://www.linuxquestions.org/questions/linux-software-2/how-to-get-the-last-sunday-of-march-and-october-in-shell-script-869370/)

dpk 03-18-2011 05:53 AM

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,

tronayne 03-18-2011 06:55 AM

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.

colucix 03-18-2011 07:18 AM

@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. :confused:

tronayne 03-18-2011 07:47 AM

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?)?

colucix 03-18-2011 07:58 AM

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.

colucix 03-18-2011 08:02 AM

Quote:

Originally Posted by tronayne (Post 4294923)
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.

colucix 03-18-2011 08:21 AM

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:

tronayne 03-18-2011 09:56 AM

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]

colucix 03-18-2011 10:01 AM

Quote:

Originally Posted by tronayne (Post 4295036)
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! ;)

archtoad6 03-18-2011 11:29 AM

Moved: This thread is more suitable in Linux - Software and has been moved accordingly to help your thread/question get the exposure it deserves.

dpk 03-18-2011 11:59 AM

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

colucix 03-18-2011 12:10 PM

Quote:

Originally Posted by dpk (Post 4295172)
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 07:03 PM.