5

Information

I have the following crontab for the root user on Debian 10.

root@debian:~# crontab -l
# crontab comments curtailed for serverfault
#
# m h  dom mon dow   command

30 3 * * * shutdown -r now

If I run uptime and who -b I get the results of the last time I did a manual reboot (yesterday around 6pm).

root@debian:~# uptime
 11:03:19 up 16:29,  1 user,  load average: 0.00, 0.01, 0.00
root@debian:~# who -b
         system boot  2020-12-26 18:34

I created the crontab around 7pm yesterday, so it was definitely in place prior to the target time.

Question

Is there a reason why this may not have worked? Can I debug this in any way?

Geesh_SO
  • 165

1 Answers1

4

Check the log to see if the cron job run at the specified time.

On Debian cron is logged in /var/log/syslog, see it there is any error reported if the cron job was activated.

If you do grep cron /var/log/syslog is there anything logged at the time cron was suppose to do reboot?

Also check if cron service is running, if you do systemctl status cron.service does it say the service is active and enabled. Cron service needs to be running in order for cron jobs to run.

It could also be an issue with cron not being able to find shutdown command, try using /usr/sbin/shutdown -r now in cron.

For Debian 12 and newer

The traditional log files have been replaced by systemd-journald in Debian 12, so you can't search in /var/log/syslog using tools line grep any more. This also affects other GNU/Linux Distributions. But you can use journalctl instead and also filter the logs directly there:

journalctl -u cron
Lion
  • 508
ralz
  • 2,781
  • Interestingly I can see the command in syslog. The one line of relevance is Dec 27 03:30:01 debian CRON[22776]: (root) CMD (shutdown -r now) but weirdly it doesn't seem to report an error. I don't know how this jibes with what I've seen with uptime and who -b. – Geesh_SO Dec 27 '20 at 12:30
  • try running mail command as root, do you have any system mail in root user inbox, on some implementaions cron will by default send system mail to user which runs cronjob if there is any error, it could be a path issue try using /usr/sbin/shutdown in cron – ralz Dec 27 '20 at 12:31
  • Found it with the mail command. Strangely running mail as root returned nothing, but running mail as my "normal" user showed me the error from the root crontab. Anyway, this is it /bin/sh: 1: shutdown: not found. Should I change shutdown to /usr/sbin/shutdown? – Geesh_SO Dec 27 '20 at 12:44
  • This changed worked. Thanks for your help. – Geesh_SO Dec 27 '20 at 13:26
  • /sbin/shutdown -r now instead of shutdown -r now fixed my cron on Ubuntu 18.04 – Igor Leonovich Jul 23 '23 at 02:09