Operating System - HP-UX
1819502 Members
3096 Online
109603 Solutions
New Discussion юеВ

How to setup cron to run on second sunday of the month?

 
SOLVED
Go to solution

How to setup cron to run on second sunday of the month?

Hi All,

This is my first time to log a question on the forum hope you can help me on this. We're thinking to have this cron entry "0(min) 8(hour) 8-14(day_of_month) 2,4,6,8,10,12(even month) 0(Sunday) command", but there some issue on our team that with this setup it will run from 8th to 14th even though not everyday is a sunday. Also, if we use this method then we will have to check it every year to make sure they are still correct.

Basically we just want to run the script every second sunday of the month.

Thanks in advance to all who will help us on this.
13 REPLIES 13
Ermin Borovac
Honored Contributor

Re: How to setup cron to run on second sunday of the month?

Something like this should do the trick

0 8 8-14 2,4,6,8,10,12 0 test `/usr/bin/date +\%w` -eq 0 && your_script

Re: How to setup cron to run on second sunday of the month?

Thanks for the trick, I think I have to re-phrase my query.

1. What entry should I put if I want to put a cronjob to reboot my server on every second sunday of the month.
2. or every second sunday of even month.

Tkhs....
Kurt Beyers.
Honored Contributor

Re: How to setup cron to run on second sunday of the month?

Romen,

Not an answer on your question but... Why do you want to perform a reboot with a crontab entry?

A reboot must only be done when it is required (eg kernel rebuild, installation of a new patch, ...) and shouldn't be done preventive, it isn't a Windows box.

And if I reboot a server, I want to follow the boot process myself or at least check that the server is up and running again.

best regards,
Kurt

Re: How to setup cron to run on second sunday of the month?

Hi Kurt,

First put to crontab entry for automation purposes. Secondly this is request by application team to at least flash or refresh the server and kill those runaway process, (even do we have script to kill those runaway, this is kind of a policy and agreed for preventive and system integrity check).

I totally agree with you that reboot must be interactively to see and check whats going on and make it sure that system is up and running, but due to lots of server we need to maintain we need to do this in automated process.

Thanks for your comment.
Borislav Perkov
Respected Contributor

Re: How to setup cron to run on second sunday of the month?

Hi,

You should put:

0 8 8-14 2,4,6,8,10,12 0 /etc/shutdown -r -y now # Rebooting only second Sunday

I used Ermin's trick.

Regards,
Borislav
Elmar P. Kolkman
Honored Contributor

Re: How to setup cron to run on second sunday of the month?

You say your team thinks it will run even though it's not sunday. Is this something you saw happening, or something you think will happen? Because it should work the way you defined it, AFAIK... All fields should match, before a cronjob is run. It's also mentioned in the manual page of crontab that this is the way to do it.
Every problem has at least one solution. Only some solutions are harder to find.
Borislav Perkov
Respected Contributor

Re: How to setup cron to run on second sunday of the month?

To be precize if you want to reboot at 08:00 it will be like this:

0 8 7-15 * 0 /etc/shutdown -r -y now

Regards

Re: How to setup cron to run on second sunday of the month?

This entry is morethan sufficient
0 8 8-14 2,4,6,8,10,12 0 /etc/shutdown -r -y 0
regards
SK
Your imagination is the preview of your life's coming attractions
Rick Garland
Honored Contributor

Re: How to setup cron to run on second sunday of the month?

The script can have the logic to check for the date/day info. Example, is the date of week = 0? Is the numeric day of the month between 08-14? Is the month = 2 or 4 or 6 or 8 or 10 or 12?

With this in the script who cares how the cron is setup.



Mohanasundaram_1
Honored Contributor
Solution

Re: How to setup cron to run on second sunday of the month?

Hi Romen,

Please see the question I posted for a similar requirement and the answers to it, below

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=593710

Also from the man pages of crontab,
------------------------------------------
ote that the
specification of days can be made in two fields: monthday and weekday.
If both are specified in an entry, they are cumulative. For example,

0 0 1,15 * 1 command

runs command at midnight on the first and fifteenth of each month, as
well as every Monday.
-------------------------------------------

In simple terms, you cannot achieve this directly through cron. You may have to write a script for achieving your requirement.

Also consider using caldj.sh. You can search with the same word in this forum and will find lots of references.

All the best.

With regards,
Mohan.
Attitude, Not aptitude, determines your altitude

Re: How to setup cron to run on second sunday of the month?

Hi All,

Thanks you guys for all your help, it was great help and to have those ideas and confirmation.
Mike Patterson
Frequent Advisor

Re: How to setup cron to run on second sunday of the month?

I agree with a combination of cron job that runs between the 8th and 14th of the month (every month in this case). The cron simply runs a script that only runs on Sunday.

0 8 8-14 * * /usr/local/rebootsundays.sh

Here is a tested script to do the job:

#!/bin/sh
#
# rebootsundays.sh
#
# If today is Sunday, reboot!
#
DAY=`date +%w`
#
# This script should be run by root:
if [ x`id | grep root | awk '{print $1}'` = "x" ]; then
echo "Root privileges are required to run `basename $0`"
exit
fi
# If it is Sunday, reboot the system.
if [ "$DAY" -eq 0 ];then
cd /
/usr/sbin/shutdown -r -y 0
else
echo "I only reboot on Sunday."
fi
A. Clay Stephenson
Acclaimed Contributor

Re: How to setup cron to run on second sunday of the month?

Have your cronjob run every Sunday and let it decide whether to simply exit or do something.

#!/usr/bin/sh

export PATH=${PATH}:/usr/local/bin

if [[ $(caljd.sh -N) -eq 2 ]]
then
echo "Second Sunday; do your thing"
else
exit 0
fi

Now if the question is to do something every other Sunday then, again, your cron job executes every Sunday;

#!/usr/bin/sh
export PATH=${PATH}:/usr/local/bin
WK=$(( ($(caljd.sh) + 1 ) / 7 ))
if [[ $((${WK} % 2)) -eq 0 ]]
then
echo "Do your thing"
else
exit 0
fi

You may need to reverse the conditions depending upon which week you want to start on; the $(caljd.sh) + 1 is used to start the week on Sunday.

Invoke as caljd.sh -u for full usage and examples.

If it ain't broke, I can fix that.