0

In the root users crontab on a Centos 7 server I have the following:

30 4 1-7 * * test $(date +\%u) -eq 7 && /usr/bin/needs-restarting -r || /usr/sbin/shutdown -r

It should run every day at 4:30 between the 1st and 7th day of the month, then it tests if the day of the week is Sunday and only then execute the next command to check if a reboot is required, and then reboot if it is. However my server rebooted today (1st Aug 2017) which is a Tuesday. Can anyone explain why?

1
  • If you're comfortable relying on a bug in cron (which will never be fixed), you can get rid of the test portion with the following schedule: 30 4 1-7 * */8 which ensures only first Sundays. So it would look like 30 4 1-7 * */8 /usr/bin/needs-restarting ... Commented Aug 1, 2017 at 13:57

1 Answer 1

2

In a && b || c, command c is executed when either a or b exit with a value other than 0. Consequently, when test $(date +\%u) -eq 7 is false, your server reboots.

According to its name /usr/bin/needs-restarting probably returns 0 when the server needs a reboot. Are you sure that this should not be a && b && c instead?

Else, try a && { b || c; }

1
  • Thanks that explains it! 'needs-restarting -r' returns 1 if a reboot is needed so I think it is a '||' I need to use. 'a && { b || c; }' seems to do this trick for me. Thanks again!
    – martin81
    Commented Aug 1, 2017 at 15:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .