Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I have 4 servers and I want each one of them to execute a reboot on Sunday.

  • Server 1 - first Sunday of the month (first week)
  • Server 2 - Second Sunday of the month (2nd week)
  • Server 3 - third Sunday of the month (3rd week)
  • Server 4 - fourth Sunday of the month (4th week)

how can I do it with Crontab? Thanks! Dotan.

share|improve this question

marked as duplicate by Bryan, Stephane, Falcon Momot, Ward, Scott Pack Jul 25 '13 at 14:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

First of all I would not configure reboot in crontab... Of course if this machines are not mission critical then ok but if they are then I would do reboot myself (rebooting four servers once a month is not much problem). After reboot some services could not start you can have errors on hdd drivers, etc. So my advice is to properly consider is it realy nessesery to reboot server via crontab – B14D3 Jul 4 '13 at 11:33
so... you don't want it to happen if there is a 5th Sunday? – warren Jul 8 '13 at 18:50
up vote 12 down vote accepted

The first Sunday will always fall between the 1st and the 7th so

10 1 1-7 * * test `date +\%a` == "Sun" &&  doSomething

will run doSomething at 10 past 1 on a Sunday that falls between the 1st and the 7th. Similarly the second Sunday will fall between the 8th and 14th, the third between 15th and 21st and the 4th between 22rd and 28th so adjust the day of month accordingly on the relevant server.

Note that the test is required because

Note: 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 cur- rent time. For example, "30 4 1,15 * 5" would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.

share|improve this answer
Hi, I added the following to my cron "0 9 8-14 * * test 'date \%a == Sun' && /sbin/reboot" I was hoping it will run next Sunday 7/14 but instead it ran today (Monday 07/08). – edotan Jul 8 '13 at 9:09
@edotan: OK I tested it and that should work as expected - but test it yourself first. – Iain Jul 8 '13 at 9:50
Hi Iain, It was supposed to run next Sunday...not today. so it's not correct. (or I'm doing something wrong...) – edotan Jul 8 '13 at 11:09
@edotan: Notice I made some changes to the crontab entry above. – Iain Jul 8 '13 at 11:27
Now I noticed :-)...Thanks a lot!!! – edotan Jul 8 '13 at 13:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.