1828658 Members
1947 Online
109983 Solutions
New Discussion

Crontab question...

 
SOLVED
Go to solution
OLIVA_1
Regular Advisor

Crontab question...

Hello,

I would like reboot automatically a server all the first sunday of the month.
I have added this line in root's crontab:

00 06 1-7 * 0 /usr/sbin/shutdown -r -y 0

But my server has rebooted July first on July 2 and on July 3... cron didn't hold account of "0" Sunday...

Someone can help me ?

Thanks,
5 REPLIES 5
Patrick Wallek
Honored Contributor

Re: Crontab question...

You need to go back and re-read the crontab man page. Cron is doing exactly what you tell it. The specifications in the crontab file for day, month and day of week are NOT 'AND'ed, they are 'OR'ed (basically).

With the schedule that you have, it will reboot your machine if the date is the 1st, 2nd, 3rd, 4th, 5th, 6th, 7th OR if it is Sunday (ANY Sunday).

In order to do what you want to do you need to build some "smarts" into your script. You can do it a couple of different ways:

1) Run your script every day the first 7 days of the month. Have a piece of code in the script check to see if it is Sunday. If it is, reboot. If it's not, don't do anything.

2) Run your script every Sunday and have your script check to see if it is the first Sunday (the date is somewhere in the range of 1 - 7). If it is, reboot, if not, don't.

Robert-Jan Goossens
Honored Contributor

Re: Crontab question...

Hi,

Or create multiple entries in the crontab.

00 06 06 08 * /usr/sbin/shutdown -r -y 0
00 06 03 09 * /usr/sbin/shutdown -r -y 0
00 06 01 10 * /usr/sbin/shutdown -r -y 0
00 06 05 11 * /usr/sbin/shutdown -r -y 0
00 06 05 11 * /usr/bin/mailx -s "Renew reboot cron Entries!" admin@xyz.com
Robert-Jan
OLIVA_1
Regular Advisor

Re: Crontab question...


Thanks to all !!!
Greg Gerke
New Member

Re: Crontab question...

The need has probably long past, but here's how rebooting on Sunday's is done around these parts:

00 17 * * 0 if [ `/bin/date +\%d` -le 7 ]; then /usr/sbin/shutdown -r -y 0 ; fi

This will reboot only the first Sunday of the month at 5pm local time. It seems... cleaner than some of the other solutions.

Regards,
Greg