You've made a couple of mistakes, all par for the course. Let's step through this, get it working, and hopefully learn one other trick to help you help yourself in the future:
First, the "other trick": When you run a command from the terminal, your error messages stderr
go to the terminal, and you see them immediately. When you run a command as a cron
user, those error messages go "somewhere else", and you don't see them when the script execution is attempted. However, you can "redirect" the stderr
to a file for post-op review later. I've modified your command to add the stderr
redirection as follows:
@reboot shutdown -h +5 > /home/pi/cronjoblog 2>&1
Copy that over your current crontab
entry. Then save your crontab
file, and reboot:
$ sudo reboot
NOTE: The $
is not an input, it's part of the command line in the shell.
Five minutes later, you'll note that your RPi has not halted, but a file containing an error message is now available here: /home/pi/cronjoblog
Inspecting this file, you'll see the following:
$ cat cronjoblog
/bin/sh: 1: shutdown: not found
$
This message simply means that the shell couldn't find shutdown
. It couldn't find it because the cron
user has a different $PATH than you do as user pi
. The solution is easy enough - simply give the full file spec for shutdown
:
@reboot /sbin/shutdown -h +5 > /home/pi/cronjoblog 2>&1
If you modify your crontab
file, then reboot again to run this, the file /home/pi/cronjoblog
will inform you of the other mistake you've made: shutdown
requires root privileges, i.e. sudo
. I'll skip the intervening trial-and-error process (but you shouldn't), and show a functional crontab
entry here:
@reboot /usr/bin/sudo /sbin/shutdown -h +5 > /home/pi/cronjoblog 2>&1
That should do it... let us know if you have other issues, and we'll address those.