Shutdown System

Post Reply
Zyox
Posts: 6
Joined: Wed Jul 30, 2025 10:00 am

Shutdown System

Post by Zyox »

Hi there,

Enjoying getting to grips with Hudiy - congrats on getting this easy to use product out there guys.
I'm running my unit currently with no safe shutdown in the car and would like to be able to easily and quickly shutdown the system.
I mainly run Android Auto, and I know it can't be done from there, but once I exit Android Auto and go back to the main Hudiy interface would it be possible to add a power button style icon to the bottom bar to the right of the time that when pressed asks do you want to shutdown or reboot and then actions that?
Thanks a million!
hudiy
Site Admin
Posts: 23
Joined: Mon Jul 14, 2025 7:42 pm

Re: Shutdown System

Post by hudiy »

Hello,
Glad to hear that, thank you!

Safe shutdown is mostly a concern when using SD cards. With USB SSD drives, we've never seen any data corruption issues on Raspberry Pi after a sudden power loss. Still, having a safe shutdown option is a good idea - just in case.

Hudiy doesn’t include built-in controls for shutting down the Raspberry Pi, but the feature you’re describing can definitely be implemented using the custom HTML/JavaScript app.

You could create a simple HTML/JavaScript app with a “Shutdown” button that triggers a sudo halt command - using a lightweight backend in Python for example (HTML/JavaScript on its own can’t run bash commands unless you're using Node.js). Sounds like an easy job for AI :).

Once the app is ready, you just need to add it to applications.json with a unique action name (like shutdown_app). Then you can link that action to the shortcuts panel or the main menu, and Hudiy will display it using WebView.

Useful links:
Examples: https://github.com/wiboma/hudiy/tree/main/examples
Applications in Hudiy: https://github.com/wiboma/hudiy/blob/ma ... plications
Menu: https://github.com/wiboma/hudiy/blob/ma ... ME.md#menu
Shortcuts: https://github.com/wiboma/hudiy/blob/ma ... #shortcuts
WebView: https://github.com/wiboma/hudiy/blob/ma ... d#web-view
Zyox
Posts: 6
Joined: Wed Jul 30, 2025 10:00 am

Re: Shutdown System

Post by Zyox »

Thanks for that.
I've tried the following:

shutdownPi.py

Code: Select all

from subprocess import call

print("Shutting Down")
call("sudo shutdown -h now", shell=True)	# tell the Pi to shut down
shutdownPi.html

Code: Select all

<!DOCTYPE html>
<html>
<head>
  <title>Shutdown Pi</title>
</head>
<body>
  <button onclick="runPythonScript()">Run Python Script</button>
  <script>
    function runPythonScript() {
      // Get the path to the Python script.
      var pythonScriptPath = "/home/pi/shutdownPi.py";
      // Run the Python script.
      subprocess.run(["python", pythonScriptPath]);
    }
  </script>
</body>
</html>
added into .hudiy/share/config/applications.json

Code: Select all

{
            "action": "shutdown_pi",
            "url": "/home/pi/shutdownPi.html",
            "allowBackground": "false",
            "controlAudioFocus": "false",
            "audioStreamCategory": "NOTIFICATION",

        },
added into .hudiy/share/config/shortcuts.json

Code: Select all

        {
            "iconFontFamily": "Material Symbols Rounded",
            "iconName": "volume_down",
            "action": "shutdown_pi"
        },
Not working however. Is there a log file or two I can look at to debug this?
Might have the HTML wrong. It's been a while since I've touched any of this and just trying to knock it up quickly so I can use it in the car.
I'm running off a SD card on Raspi4 and bootup time is <30s so I'm happy enough for now but do need to implement a safe shutdown.
hudiy
Site Admin
Posts: 23
Joined: Mon Jul 14, 2025 7:42 pm

Re: Shutdown System

Post by hudiy »

There is no subprocess available in HTML/JavaScript. The simplest approach is to handle everything in Python. In this minimal example, a Python script starts a basic HTTP server, serves HTML with a "Shutdown" button, and executes the sudo halt command when the button is clicked.

The script below runs the HTTP server on port 9999 and serves a basic HTML page containing mentioned single "Shutdown" button.

Code: Select all

from http.server import BaseHTTPRequestHandler, HTTPServer
import subprocess

PORT = 9999

HTML_PAGE = b"""<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Shutdown</title>
</head>
<body style="font-family:sans-serif; text-align:center; margin-top:50px;">
  <button onclick="sendShutdown()" style="font-size:24px; padding:10px 20px;">Shutdown</button>
  <p id="status"></p>
  <script>
    function sendShutdown() {
      fetch("/shutdown", { method: "POST" })
        .then(r => r.text()).then(t => {
          document.getElementById("status").innerText = t;
        }).catch(e => {
          document.getElementById("status").innerText = "Error: " + e;
        });
    }
  </script>
</body>
</html>
"""

class ShutdownHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == "/":
            self.send_response(200)
            self.send_header("Content-Type", "text/html; charset=utf-8")
            self.send_header("Content-Length", str(len(HTML_PAGE)))
            self.end_headers()
            self.wfile.write(HTML_PAGE)
        else:
            self.send_error(404, "Not Found")

    def do_POST(self):
        if self.path == "/shutdown":
            self.send_response(200)
            self.end_headers()
            self.wfile.write(b"Shutdown command issued")
            try:
                subprocess.run(["sudo", "/sbin/halt"], check=True)
            except Exception as e:
                print(f"Error: {e}")
        else:
            self.send_error(404, "Not Found")

    def log_message(self, format, *args):
        return

if __name__ == "__main__":
    print(f"Starting server on http://0.0.0.0:{PORT}")
    with HTTPServer(("", PORT), ShutdownHandler) as httpd:
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            print("\nServer stopped.")
Then, in applications.json (applications array), you can add the following entry (make sure the json syntax is correct):

Code: Select all

        {
            "action": "shutdown_pi",
            "url": "http://127.0.0.1:9999",
            "allowBackground": "false",
            "controlAudioFocus": "false"
        }
and in shortcuts.json (shortcuts array)

Code: Select all

        {
            "iconFontFamily": "Material Symbols Rounded",
            "iconName": "power_settings_new",
            "action": "shutdown_pi"
        }
List of glyphs in Material Symbols Rounded font is available at https://fonts.google.com/icons

To autostart the script you can add it to the /etc/xdg/labwc/autostart file, for example:

Code: Select all

/path/to/your/script.py &
Note the & at the end - it runs the script in the background, so it doesn't block the execution of other commands in the /etc/xdg/labwc/autostart file.

Of course, a more sophisticated HTML layout can be created to better match the Hudiy color scheme (see https://github.com/wiboma/hudiy?tab=rea ... diy-object). However, the current version should serve as a good starting point. We've added the creation of such an enhanced example to our backlog, and it will be available on our GitHub soon.
Zyox
Posts: 6
Joined: Wed Jul 30, 2025 10:00 am

Re: Shutdown System

Post by Zyox »

Tried this but I just get a large red screen with a "!" on it when I press the new power button shortcut. Obviously some issue with the python script. Is there some log file I can check for more info? Thanks.
hudiy
Site Admin
Posts: 23
Joined: Mon Jul 14, 2025 7:42 pm

Re: Shutdown System

Post by hudiy »

If you see such screen it means the url (http://127.0.0.1:9999) is not available. Try to reach http://127.0.0.1:9999 from web browser or via curl command.

What is the output of the python script when you run it from the terminal by python3 /path/to/script.py command?

To run the script from /etc/xdg/labwc/autostart, make sure the file has executable permissions (chmod +x /path/to/script.py) and starts with the following shebang (https://en.wikipedia.org/wiki/Shebang_(Unix)) line to ensure it's executed using the Python 3 interpreter:

Code: Select all

#!/usr/bin/env python3
otherwise it must be added as /usr/bin/python3 /path/to/script.py & in /etc/xdg/labwc/autostart
Zyox
Posts: 6
Joined: Wed Jul 30, 2025 10:00 am

Re: Shutdown System

Post by Zyox »

Yup it just needed the "#!/usr/bin/env python3" added to the beginning of the file. Thank you. That will work great as a stop gap.
Post Reply