Page 1 of 1

Shutdown Raspberry Pi with Python Script

Posted: Fri Jan 22, 2016 10:21 pm
by marinahanna
I've been looking into ways of turning off the Raspberry Pi solely by running a python script. I've mostly seen people attach a button and program the GPIO pins. But is there anyway to only turn it off through a python script and not through the use of a button? Thanks!

Re: Shutdown Raspberry Pi with Python Script

Posted: Fri Jan 22, 2016 10:34 pm
by kusti8

Code: Select all

from subprocess import call
call("sudo shutdown -h now", shell=True)

Re: Shutdown Raspberry Pi with Python Script

Posted: Fri Jan 22, 2016 11:07 pm
by marinahanna
Thanks for the response! I have a couple of questions, though. Whenever I run this script, it shuts down but restarts. I was looking for a command that would run "poweroff". I replaced "shutdown" with "poweroff" but that didn't work. How come it restarts with the "shutdown" command? And why does "poweroff" not work with this line of code?

Thanks!

EDIT: I ran this code instead:

Code: Select all

from subprocess import call
call("sudo poweroff", shell=True)
The Raspberry Pi automatically restarts after I run the script, still not sure why.

Re: Shutdown Raspberry Pi with Python Script

Posted: Sat Jan 23, 2016 12:25 am
by rurwin
Try:

Code: Select all

    from subprocess import call
    call("sudo nohup shutdown -h now", shell=True)
The -h flag tells it to halt, just like poweroff.

I'll give this a try in the morning and see if I can reproduce it. How are you running the script? From IDLE, a command window, or a console without the GUI?

Re: Shutdown Raspberry Pi with Python Script

Posted: Sat Jan 23, 2016 11:15 am
by DougieLawson
rurwin wrote:Try:

Code: Select all

    from subprocess import call
    call("sudo nohup shutdown -h now", shell=True)
The -h flag tells it to halt, just like poweroff.
Not quite.

The -h flag says "poweroff" unless you also say --halt (or -H)
sudo shutdown -P now
or
sudo shutdown --poweroff
is less ambiguous and does the software poweroff stuff (doesn't do a hardware poweroff).

Re: Shutdown Raspberry Pi with Python Script

Posted: Sat Jan 23, 2016 11:22 am
by GTR2Fan
DougieLawson wrote:sudo shutdown --poweroff
is less ambiguous and does the software poweroff stuff (doesn't do a hardware poweroff).
Would a sudo sync be advisable before this, or is that effectively taken care of already? TIA.

Re: Shutdown Raspberry Pi with Python Script

Posted: Sat Jan 23, 2016 5:23 pm
by DougieLawson
Nope. Not needed an orderly shutdown will sync the filesystems before shooting the kernel dead.

Re: Shutdown Raspberry Pi with Python Script

Posted: Sat Jan 23, 2016 8:23 pm
by rpdom
Also, sync does not require sudo. In fact it partly defeats the object by doing so, as sync is supposed to flush all buffers to "disk", but sudo then writes to a log file afterwards.

Re: Shutdown Raspberry Pi with Python Script

Posted: Sat Jan 23, 2016 8:28 pm
by GTR2Fan
I am further enlightened. Thank you chaps. :)

Re: Shutdown Raspberry Pi with Python Script

Posted: Sat Jan 23, 2016 9:24 pm
by rurwin
I just ran marinahanna's code both from SSH and from IDLE and both times the Pi shutdown correctly.

This is a Pi2B running stock and current Raspbian. (I'm upgrading it now but, as the SD card is full to the brim, I've a nasty feeling I'm just corrupting all 58 packages. Time to buy some 16GB microSDs...)

Re: Shutdown Raspberry Pi with Python Script

Posted: Mon Jan 25, 2016 4:53 pm
by marinahanna
Hi guys,

So I also tried what was advised:

Code: Select all

from subprocess import call
    call("sudo nohup shutdown -h now", shell=True)
However, it still shuts down and restarts again. In this case, I should explain a little bit more about how I've got my Raspberry Pi set up and perhaps you might be able to aid me in this.

The Raspberry Pi is connected to this Adafruit TFT+Touchscreen for Raspberry Pi (https://www.adafruit.com/products/1601). I can imagine that the touchscreen probably has something to do with why it won't shut off when running that command, but I'm not experienced enough in knowing what might be causing the issue. The Raspberry Pi is being powered by the adafruit powerboost 1000c, with an LiIon battery attached.

I am also running the code off IDLE. However, when I run these from the terminal:

Code: Select all

sudo shutdown -h now
sudo shutdown -P now
sudo poweroff
sudo halt -p
sudo init 0 
the same thing happens and the RPi turns on after shutdown or poweroff. Not sure what else to try at this point. Any help is appreciated!

Re: Shutdown Raspberry Pi with Python Script

Posted: Mon Jan 25, 2016 5:41 pm
by rurwin
As I said, it doesn't happen on a bare Pi2. And everyone would have a problem if it happened with the command-line versions.

It may be worth posting on the Adafruit forums or emailing them.

Can you try it without one or the other Adafruit add-ons?