6

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.

quanta
  • 50,327
  • 19
  • 152
  • 213
edotan
  • 1,786
  • 12
  • 37
  • 57
  • 1
    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 04 '13 at 11:33
  • so... you don't want it to happen if there is a 5th Sunday? – warren Jul 08 '13 at 18:50

1 Answers1

12

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.

user9517
  • 114,104
  • 20
  • 206
  • 289
  • 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 08 '13 at 09:09
  • @edotan: OK I tested it and that should work as expected - but test it yourself first. – user9517 Jul 08 '13 at 09: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 08 '13 at 11:09
  • @edotan: Notice I made some changes to the crontab entry above. – user9517 Jul 08 '13 at 11:27