1

I'm trying to configure a periodic reboot of my Raspberry Pi 4 via crontab (reboot every Wednesday and Sunday at midnight). However, the Raspi actually never reboots.

Here's a summary of my different attempts:

  • sudo crontab -e0 0 * * WED,SUN reboot → ❌ no reboot
  • sudo crontab -e0 0 * * WED,SUN sudo reboot → ❌ no reboot
  • crontab -e0 0 * * WED,SUN reboot → ❌ no reboot
  • crontab -e0 0 * * WED,SUN sudo reboot → ❌ no reboot
  • sudo crontab -e0 0 * * WED,SUN shutdown -r now → ❌ no reboot

Since none of the previous attempts worked, I tried the following:

  • sudo crontab -e0/10 * * * * reboot >> /home/myUser/temp/reboot.log → ❌ no reboot and /home/myUser/temp/reboot.log does not exist

The output of cat /var/log/syslog | grep -w 'cron' over the course of three hours is:

Nov  9 14:28:01 myRaspi cron[405]: (myUser) RELOAD (crontabs/myUser)
Nov  9 14:28:01 myRaspi cron[405]: (root) RELOAD (crontabs/root)
Nov  9 15:17:01 myRaspi CRON[15534]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Nov  9 16:17:01 myRaspi CRON[27095]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)

Here's the output of cat /etc/os-release:

PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
0

2 Answers 2

1

I think you've got the syntax right on the first entry:

  • sudo crontab -e0 0 * * WED,SUN reboot → ❌ no reboot

I confirmed that with crontab guru, and in man 5 crontab.

The thing you have missed is in the $PATH for cron. cron's PATH is PATH=/usr/bin:/bin, and reboot is at /usr/sbin/reboot.

Consequently, if you'll change the root crontab entry as follows, it should work:

0 0 * * WED,SUN /usr/sbin/reboot

Also, you can generally help yourself out with crontab entries by adding some re-direction to capture the stderr and stdout streams to a file (because cron cannot know how to reach your terminal):

0 0 * * WED,SUN /usr/sbin/reboot >> /home/pi/cronlog 2>&1
6
  • Price for the tech hero of the day goes to you :) it works, thank you very much! Just for my understanding: the file cronlog does not contain any content after the reboot - should it?
    – mu88
    Nov 11, 2023 at 14:06
  • Yes. Cron runs in a restricted environment and there is no $PATH to search for executables specified by name only. Nov 11, 2023 at 14:20
  • Sorry, but I don't understand how that answers my question why the log file cronlog that Seamus suggested is empty?
    – mu88
    Nov 11, 2023 at 14:27
  • 1
    @mu88: Glad it helped. If /home/pi/cronlog does not contain any content, that is because your command (/usr/sbin/reboot) did not generate any... but of course it wouldn't if the command were successful. But that's not generally true for all commands. The point is that this redirection ( >> ) should always be used if you need to capture any output from your command. You might actually try the redirection on some of the jobs you tried that did not work to see what it has to say :)
    – Seamus
    Nov 11, 2023 at 14:50
  • never use a sudo crontab. Had a problem with it in past, a normal crontab -e works just fine. Nov 21, 2023 at 21:35
2

The correct syntax would be:

0 0 * * 0,3 /sbin/reboot

in the crontab for root.

As for what you are doing:

sudo crontab -e → 0 0 * * WED,SUN reboot → ❌ no reboot

You're editing the crontab for root and put the reboot in there.

sudo crontab -e → 0 0 * * WED,SUN sudo reboot → ❌ no reboot

You're editing the crontab for root and put in sudo reboot. Because the cronjobs get executed by root, the sudo is unnecessary.

crontab -e → 0 0 * * WED,SUN reboot → ❌ no reboot

You're editing the user's crontab an put in a reboot. The ordinary user is not allowed to reboot and therefore, this will fail.

crontab -e → 0 0 * * WED,SUN sudo reboot → ❌ no reboot

You put a sudo reboot in the user's crontab. If the user is alllowed to sudo reboot, then this might work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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