Page 1 of 1

How do I implement a Linux shutdown -h now command from pyth

Posted: Tue Feb 17, 2015 9:32 pm
by americantrain
I am trying to safely shutdown Pi using a button signal through the GPIO. When I include the command "shutdown -h now" in the python script it either gives me a syntax error or says that "shutdown" is not defined. What am I doing wrong and how can I fix it.

A sample of the Code is attached:

.[import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
While True:
If GPIO.input(23):
shutdown -h now]

Re: How do I implement a Linux shutdown -h now command from

Posted: Tue Feb 17, 2015 9:39 pm
by elParaguayo
shutdown is a shell comand, not a python one.

To use shell commands in python you should use the subprocess module. e.g.

Code: Select all

import subprocess
subprocess.call(["shutdown", "-h", "now"])
I would suggest, however, that you replace that line with a print statement (or something similar) so you can check the rest of the code is working without shutting down your pi every time. Once it's working, then use the subprocess line.

You may run into permission issues depending on how you're running your python script.

Re: How do I implement a Linux shutdown -h now command from

Posted: Tue Feb 17, 2015 10:13 pm
by kusti8
elParaguayo wrote: You may run into permission issues depending on how you're running your python script.
You have to run the python script as root anyway if you are using GPIO. Anyway, yes, that is the way to go.

Re: How do I implement a Linux shutdown -h now command from

Posted: Tue Feb 17, 2015 10:14 pm
by elParaguayo
kusti8 wrote:
elParaguayo wrote: You may run into permission issues depending on how you're running your python script.
You have to run the python script as root anyway if you are using GPIO. Anyway, yes, that is the way to go.
Well, yes, that was dumb of me to miss that. Thanks for putting me straight!

Re: How do I implement a Linux shutdown -h now command from

Posted: Sat Sep 24, 2016 4:19 am
by Ace Rimmer
Hi, I've been trying to use the above code to shutdown my pi when python script finishes. When the script finishes instead of shutting down I get the following

TypeError: 'str' object is not callable

What am I doing wrong?

Re: How do I implement a Linux shutdown -h now command from

Posted: Sat Sep 24, 2016 6:11 am
by Ace Rimmer
I just tried

Code: Select all

os.system('shutdown now -h')
and it works a treat :D

Re: How do I implement a Linux shutdown -h now command from

Posted: Sat Sep 24, 2016 6:25 am
by elParaguayo
Ace Rimmer wrote:Hi, I've been trying to use the above code to shutdown my pi when python script finishes. When the script finishes instead of shutting down I get the following

TypeError: 'str' object is not callable

What am I doing wrong?
Post your code and we'll find the problem. It would be good to fix this as using subprocess is preferred over os.system.

Re: How do I implement a Linux shutdown -h now command from

Posted: Sat Sep 24, 2016 7:09 am
by tom.slick
The shlex will give you the correct args to pass to subprocess.call

Code: Select all

import subprocess
import shlex
cmd = shlex.split("sudo shutdown -h now")
subprocess.call(cmd)

Re: How do I implement a Linux shutdown -h now command from

Posted: Sat Sep 24, 2016 7:21 am
by elParaguayo
tom.slick wrote:The shlex will give you the correct args to pass to subprocess.call

Code: Select all

import subprocess
import shlex
cmd = shlex.split("sudo shutdown -h now")
subprocess.call(cmd)
Indeed - it's a very good way of doing this when you have a command line that you know works.

I don't know if that would cause the error described though.

Re: How do I implement a Linux shutdown -h now command from

Posted: Sat Sep 24, 2016 10:01 pm
by Ace Rimmer
Hi again, the code I used was exactly as the above example

Code: Select all

import subprocess
.
.#my code
.
subprocess.call(["shutdown", "-h", "now"])
I tried several variations, all to no avail. I don't know if it makes any difference but I'm using Python 2 under Wheezy.

Re: How do I implement a Linux shutdown -h now command from

Posted: Sun Sep 25, 2016 5:08 am
by elParaguayo
That shouldn't give you the error message you saw. Can you paste the full error message (it should refer to the offending line) and paste the rest of your code.

Re: How do I implement a Linux shutdown -h now command from

Posted: Sun Sep 25, 2016 6:35 am
by tom.slick
elParaguayo wrote:
tom.slick wrote:The shlex will give you the correct args to pass to subprocess.call

Code: Select all

import subprocess
import shlex
cmd = shlex.split("sudo shutdown -h now")
subprocess.call(cmd)
Indeed - it's a very good way of doing this when you have a command line that you know works.

I don't know if that would cause the error described though.
That advise is for the OP, just to make things a little easier for the future.

To Rimmer:
elParaguayo wrote:That shouldn't give you the error message you saw. Can you paste the full error message (it should refer to the offending line) and paste the rest of your code.
As stated, give the entire error message and code used. A string error should not be happening.

Re: How do I implement a Linux shutdown -h now command from

Posted: Sun Sep 25, 2016 9:32 pm
by Ace Rimmer
Thanks Tom & elP' , may take a while to get things back the way they were in order to replicate the error.
Cheers.

Re: How do I implement a Linux shutdown -h now command from

Posted: Fri Sep 30, 2016 12:05 am
by Ace Rimmer
Better late than never. Below isn't the original bit of code I was using but it produces the same errorcode.

Code: Select all

 #!/usr/bin/env python2.7
print ('   INITIALISING >>>>>>')

import datetime as dt
from time import *#time
import RPi.GPIO as GPIO
import subprocess

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18, GPIO.OUT) # Red LED

def blinkescape():
    x=0
    while x < 10:
    
        GPIO.output(18, GPIO.HIGH)
        sleep(1)
        GPIO.output(18, GPIO.LOW)
        sleep(1)
        x=x+1
blinkescape()

print ("goodbye")
sleep(1)
subprocess.call(["shutdown", "-h", "now"])

and the result....

Code: Select all

WELCOME TO MY NIGHTMARE  Fri Sep 30 09:50:40 AEST 2016


[email protected] ~ $ sudo python2 blinkescape.py
   INITIALISING >>>>>>
goodbye
Traceback (most recent call last):
  File "blinkescape.py", line 26, in <module>
    subprocess.call(["shutdown", "-h", "now"])
TypeError: 'str' object is not callable
[email protected] ~ $ 
and there you have it!
As I said in my earlier post I'm using Python2 under Wheezy on a B+.

Re: How do I implement a Linux shutdown -h now command from

Posted: Fri Sep 30, 2016 1:21 am
by tom.slick
I see the problem
Fri Sep 30 09:50:40 AEST 2016

Your RPI is upside down.

But seriously the script works for me. Do you maybe have a file called subprocess.py in the same folder.
Add these lines to just before subprocess.call(["shutdown", "-h", "now"])
print subprocees
print type(subprocess)
print subprocess.call
print type(subprocess.call)

Re: How do I implement a Linux shutdown -h now command from

Posted: Fri Sep 30, 2016 2:05 am
by Ace Rimmer
Thanks tom, you're a genius.
Before I added the extra bit of code that you suggested I had a look in /Pi and sure enough there was a file called subprocess.pyc. That jogged my memory, I made that file in one of my many attempts to get "shutdown" happening and forgot to delete it.

The print out after the extra code......

Code: Select all

WELCOME TO MY NIGHTMARE  Fri Sep 30 11:49:44 AEST 2016


[email protected] ~ $ sudo python2 blinkescape.py
   INITIALISING >>>>>>
goodbye
<module 'subprocess' from '/home/pi/subprocess.pyc'>
<type 'module'>
sudo shutdown --P
<type 'str'>
Traceback (most recent call last):
  File "blinkescape.py", line 30, in <module>
    subprocess.call(["shutdown", "-h", "now"])
TypeError: 'str' object is not callable
[email protected] ~ $ 

You can see why most of my posts are in "Beginners" :roll:
I think it might be me that is upside down!!

Re: How do I implement a Linux shutdown -h now command from

Posted: Fri Sep 30, 2016 2:13 am
by Ace Rimmer
I almost made the mistake of deleting the offending file and running the script again while I was half way through my reply.
A good thing I woke up in time as it now shuts down as it should. 8-)
Thanks again.

Re: How do I implement a Linux shutdown -h now command from

Posted: Fri Sep 30, 2016 7:22 am
by tom.slick
So to recap for the benifet of others, you were importing a file you named subrpocess and not the built-in subprocess module.

print subprocess
<module 'subprocess' from '/home/pi/subprocess.pyc'>
/home/pi/subprocess.pyc is the location of an old file you made and not something from the python path

print type(subporcess)
<type 'module'>
it's still a module just like the subprocess module, but one that you wrote

print subprocess.call
sudo shutdown --P
the result of your module having a variable call that was set to "sudo shutdown --P"

print type(subprocess.call)
<type 'str'>
confirm that it was a string in your module

deleting or moving subprocess.py and/or subprocess.pyc solved your problem as now you import the built-in subprocess module instead of your own module.

Re: How do I implement a Linux shutdown -h now command from

Posted: Fri Sep 30, 2016 9:50 pm
by Ace Rimmer
That's right tom, when the module I wrote didn't work I neglected to delete it before I moved to the next method. Mea culpa.

Re: How do I implement a Linux shutdown -h now command from

Posted: Sat Oct 01, 2016 5:56 am
by fdr4prez
I've successfully used the two samples for shutdown

This code shutdowns when you push a button:

Code: Select all

import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BOARD)
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP)
while True:
    print GPIO.input(5)
    if(GPIO.input(5) == False):
        os.system(“sudo shutdown -h now”)
        break
    time.sleep(1)
This code shutdowns if you hold down a button for 2 seconds:

Code: Select all

from gpiozero import Button
from subprocess import check_call
from signal import pause

def shutdown():
    check_call(['sudo', 'poweroff'])

shutdown_btn = Button(19, pull_up=True, hold_time=2) #change 19 into desired GPIO pin
shutdown_btn.when_held = shutdown

pause()