americantrain
Posts: 10
Joined: Tue Jan 20, 2015 8:41 pm

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

Tue Feb 17, 2015 9:32 pm

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]

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

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

Tue Feb 17, 2015 9:39 pm

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.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

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

Tue Feb 17, 2015 10:13 pm

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.
There are 10 types of people: those who understand binary and those who don't.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

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

Tue Feb 17, 2015 10:14 pm

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!
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
Ace Rimmer
Posts: 104
Joined: Wed May 27, 2015 1:51 am
Location: Wit's End

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

Sat Sep 24, 2016 4:19 am

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?
There must be an easier way!!!!!
I hope there is.....I haven't found it yet.

User avatar
Ace Rimmer
Posts: 104
Joined: Wed May 27, 2015 1:51 am
Location: Wit's End

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

Sat Sep 24, 2016 6:11 am

I just tried

Code: Select all

os.system('shutdown now -h')
and it works a treat :D
There must be an easier way!!!!!
I hope there is.....I haven't found it yet.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

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

Sat Sep 24, 2016 6:25 am

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.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

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

Sat Sep 24, 2016 7:09 am

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)

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

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

Sat Sep 24, 2016 7:21 am

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.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
Ace Rimmer
Posts: 104
Joined: Wed May 27, 2015 1:51 am
Location: Wit's End

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

Sat Sep 24, 2016 10:01 pm

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.
There must be an easier way!!!!!
I hope there is.....I haven't found it yet.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

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

Sun Sep 25, 2016 5:08 am

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.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

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

Sun Sep 25, 2016 6:35 am

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.

User avatar
Ace Rimmer
Posts: 104
Joined: Wed May 27, 2015 1:51 am
Location: Wit's End

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

Sun Sep 25, 2016 9:32 pm

Thanks Tom & elP' , may take a while to get things back the way they were in order to replicate the error.
Cheers.
There must be an easier way!!!!!
I hope there is.....I haven't found it yet.

User avatar
Ace Rimmer
Posts: 104
Joined: Wed May 27, 2015 1:51 am
Location: Wit's End

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

Fri Sep 30, 2016 12:05 am

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+.
There must be an easier way!!!!!
I hope there is.....I haven't found it yet.

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

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

Fri Sep 30, 2016 1:21 am

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)

User avatar
Ace Rimmer
Posts: 104
Joined: Wed May 27, 2015 1:51 am
Location: Wit's End

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

Fri Sep 30, 2016 2:05 am

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!!
There must be an easier way!!!!!
I hope there is.....I haven't found it yet.

User avatar
Ace Rimmer
Posts: 104
Joined: Wed May 27, 2015 1:51 am
Location: Wit's End

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

Fri Sep 30, 2016 2:13 am

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.
There must be an easier way!!!!!
I hope there is.....I haven't found it yet.

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

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

Fri Sep 30, 2016 7:22 am

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.

User avatar
Ace Rimmer
Posts: 104
Joined: Wed May 27, 2015 1:51 am
Location: Wit's End

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

Fri Sep 30, 2016 9:50 pm

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.
There must be an easier way!!!!!
I hope there is.....I haven't found it yet.

fdr4prez
Posts: 17
Joined: Fri May 13, 2016 8:06 pm

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

Sat Oct 01, 2016 5:56 am

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()

Return to “Python”