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 -rIt 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?
Cron Job executing on the wrong day
centos;scripting;cron
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; }