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

Description=Serve a configuration website

ExecStart=/usr/bin/python3 /home/pi/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 method='post'>
                <button name='restart' value='poweoff'>Poweroff</button>
        return html 

    def get(self):

    def post(self):
        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')

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

if __name__ == '__main__':
    app = make_app()
    app.listen(8000, address='')
import os
import subprocess

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.