2

I'm looking to have my pi shutdown automatically based on a time interval (rather than the time of day). This is for an art installation and the reason I am turning off the pi is because it will connected to a projector, and to get the projector to shutoff, it needs to not be receiving a signal. I've tried editing cron with the follow command to no success:

@reboot shutdown -h +5

This was the test sample, my goal is have it shutdown after 3 hours. This is to save lamp life as the sculpture will run continuously, 24/7. I have a timer switch connected to the pi and projector that will turn it back on automatically.

Is there another way to do this?

1
  • AFAIK the Pi will continue to output video even when shutdown. You need to remove power.
    – Milliways
    Commented Feb 1, 2019 at 22:07

1 Answer 1

5

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.

5
  • When adding the line by running sudo crontab -e you don't need to put sudo to your crontab.
    – jake
    Commented Feb 2, 2019 at 3:45
  • @jake: What do you suppose the advantage is for running sudo crontab -e vs. running shutdown as root?
    – Seamus
    Commented Feb 2, 2019 at 5:28
  • Nothing, if you are allowed to sudo without a password!
    – jake
    Commented Feb 2, 2019 at 12:28
  • @jake: AFAIK, user pi needs no pwd for sudo
    – Seamus
    Commented Feb 3, 2019 at 0:30
  • Thanks! This worked. As a stubborn guy who never asks for directions, I have been struggling with this for a long time.
    – Scott Goss
    Commented Feb 4, 2019 at 15:53

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.