raspmangobr
Posts: 13
Joined: Tue Nov 04, 2014 6:59 pm

Rebooting raspbian via Python

Wed Jan 21, 2015 9:04 am

Hello everyone,
I am desperately trying to find out a way to force my Raspberry Pi running Raspbian to restart when a certain condition is met (Python script), however I got no success so far...

I have tried the following statements by using popen:

Code: Select all

sudo reboot -i -p
sudo reboot -f
sudo shutdown -r -f now

Code: Select all

from subprocess import Popen, PIPE

def reboot():
    echo.echo("Rebooting...")
    db.write_alarm(get_alarm_status())
    upload.upload_log()
    reboot_statement = "sudo shutdown -r -f now"
    popen_args = reboot_statement.split(" ")
    Popen(popen_args, stdout=PIPE, stderr=PIPE)
    
I thought the problem could be calling it through the Python application itself, therefore I wrote a small C program to kill all running Python application and then reboot, but no success...

My Raspberry is enough powered (Red LED is always on) and all commands I described above work fine when called directly from the command window.

Any help is appreciated!

Thanks,

User avatar
DougieLawson
Posts: 33603
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Rebooting raspbian via Python

Wed Jan 21, 2015 9:47 am

raspmangobr wrote:
I have tried the following statements by using popen:
Since there's nothing left to do if the shutdown command works there's no point using popen (which is deprecated in favour of subprocess anyway). You don't need to do anything with the output from a successful shutdown command.

So switch to

Code: Select all

import os
os.system('sudo shutdown -r now')
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Return to “Troubleshooting”

Who is online

Users browsing this forum: Baidu [Spider], drgeoff, prabhat, raspTest and 55 guests