1

I'm having some issues powering down the Raspberry Pi through a web interface.

I have a Tornado server which basically serves a button, and upon clicking it executes the following command:

subprocess.os.system('sudo shutdown -h now')

Upon clicking the button, the Pi powers down, then I have a blank screen with a cursor blinking, and after a few seconds, it powers back up.

The strange thing is that if I just open a Python3 terminal and issue the same command, it properly powers down.

I'm powering the Pi from a USB charger.

Any idea why I get this different behaviour?

Additional info

The server is run as a systemd process

[Unit]
Description=Serve a configuration website
After=network.target

[Service]
Type=simple
User=pi
ExecStart=/usr/bin/python3 /home/pi/pi_server.py
Restart=always

[Install]
WantedBy=multi-user.target

pi_server.py

# -*- coding: utf-8 -*-

import tornado.ioloop
import tornado.web
import wifi
import subprocess

class MainHandler(tornado.web.RequestHandler):

    def _create_form(self):
        html = '''
            <form method='post'>
                <button name='restart' value='restart'>Restart</button>
            </form>
            <form method='post'>
                <button name='restart' value='poweoff'>Poweroff</button>
            </form>
        '''
        return html 

    def get(self):
        self.write(self._create_form())

    def post(self):
        print('clicked?')
        for key in self.request.arguments:
            if 'restart' in key:
                subprocess.os.system('sudo shutdown -r now')
            elif 'poweroff' in key:
                subprocess.os.system('sudo shutdown -h now')
        self.write(self._create_form())

def make_app():
    return tornado.web.Application([
        (r'/', MainHandler),
    ])

if __name__ == '__main__':
    app = make_app()
    app.listen(8000, address='0.0.0.0')
    tornado.ioloop.IOLoop.current().start()
import os
import subprocess
2

Look at this answer.

Basically, you need to give your buttons different names, not different values. If you look at your if statement, the first condition is 'restart', and both your buttons are named 'restart'.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.