Turn off your Raspberry Pi from Anywhere with This Simple Python Script

Learn how to remotely shut down or restart your Raspberry Pi using a simple Python script that utilizes SSH. …


Updated August 27, 2023

Need help with your Raspberry Pi?
Contact Me!

Do you love silly Raspberry Pi Projects?
Check out my this YouTube Channel!


Learn how to remotely shut down or restart your Raspberry Pi using a simple Python script that utilizes SSH.

First, make sure that you have set up SSH on your Raspberry Pi. If you haven’t done so yet, follow these steps:

  1. Open the terminal on your Raspberry Pi and type sudo raspi-config.
  2. Navigate to “Interfacing Options” and enable SSH.
  3. Exit and save the changes by selecting “Finish”.
  4. Note down your Raspberry Pi’s local IP address, which can be found in the terminal by typing hostname -I.

Next, you need to connect to your Raspberry Pi via SSH from another computer or device on the same network. If you are using a Windows machine, download and install PuTTY from here. For Mac and Linux users, open Terminal and type ssh pi@your_raspberry_pi_ip (replace “your_raspberry_pi_ip” with the actual IP address of your Pi). When prompted for a password, enter raspberry.

Now that you have established an SSH connection to your Raspberry Pi, let’s create a simple Python script that will allow us to shut it down remotely. Create a new file called shutdown_pi.py and add the following code:

import os
import sys

def shutdown():
    os.system("sudo shutdown -h now")

def restart():
    os.system("sudo reboot")

if len(sys.argv) < 2 or (sys.argv[1] != "shutdown" and sys.argv[1] != "restart"):
    print("Usage: python shutdown_pi.py [shutdown|restart]")
else:
    if sys.argv[1] == "shutdown":
        shutdown()
    elif sys.argv[1] == "restart":
        restart()

This script defines two functions, shutdown() and restart(), which execute the appropriate Linux commands to turn off or restart your Raspberry Pi. The if statement checks if the user has provided an argument (“shutdown” or “restart”) when running the script, and calls the corresponding function accordingly.

Make the script executable by typing chmod +x shutdown_pi.py. Now you can run it with ./shutdown_pi.py [shutdown|restart]. For example, to shut down your Raspberry Pi, type ./shutdown_pi.py shutdown.

Finally, let’s set up a reverse SSH tunnel so that you can run the shutdown_pi.py script from anywhere in the world. First, install autossh on your Raspberry Pi with sudo apt-get install autossh. Then create an account on a cloud server (such as DigitalOcean or AWS) and set up SSH access to it using the same method described above.

On your Raspberry Pi, type autossh -M 0 -f -N -R 22222:localhost:22 your_cloud_server_username@your_cloud_server_ip (replace “your_cloud_server_username” and “your_cloud_server_ip” with the actual values). This will create a reverse SSH tunnel that forwards port 22222 on your cloud server to port 22 on your Raspberry Pi.

Now you can run the shutdown_pi.py script remotely by connecting to your cloud server using PuTTY or Terminal (ssh -p 22222 pi@your_cloud_server_ip) and running ./shutdown_pi.py [shutdown|restart]. When you need to turn off your Raspberry Pi, simply execute this command from anywhere in the world!

And that’s it! You now have a simple Python script that allows you to remotely shut down or restart your Raspberry Pi using SSH. This can be useful for saving energy, automating tasks, or just having peace of mind knowing that you can turn off your Pi from afar.