Shutdown scripts for RetroPie


The default SSH user name is pi, password raspberry.

If you get an error 'Connection refused' when you try to connect using SSH, then you need to enable SSH first, it is disabled by default on newer versions of RetroPie. To enable it you will either need to physically log in and enable it with sudo raspi-config, menu Interfacing Options, ssh or insert your sd card in a computer and create a file called ssh with no filename extension in the root folder.

  • Key in :
  • cd /etc
    sudo nano rc.local
  • Copy and paste (use shift+ins to paste in nano) the following into the nano editor window, just above the last line (the line exit 0)
    (/etc/irswitch.sh)&
  • Press ctrl+x to exit the editor, y to confirm, enter to save the file
  • Key in
    sudo nano irswitch.sh
  • Copy and paste the following text into the editor window
    #!/bin/bash
    # this is the GPIO pin receiving the shut-down signal
    GPIOpin1=14
    echo "$GPIOpin1" > /sys/class/gpio/export
    echo "in" > /sys/class/gpio/gpio$GPIOpin1/direction
    while true; do
      sleep 1
      power=$(cat /sys/class/gpio/gpio$GPIOpin1/value)
      if [ $power != 0 ]; then
        echo "out" > /sys/class/gpio/gpio$GPIOpin1/direction
        echo "1" > /sys/class/gpio/gpio$GPIOpin1/value
        sleep 3
        # the following line shuts down ES properly, if running
        /etc/multi_switch.sh --es-poweroff
        sudo shutdown -h now
      fi
    done
  • Press ctrl+x to exit, y to confirm, enter to save the file
  • Key in
    sudo chmod +x irswitch.sh
  • In order to properly shutdown the RetroPi ES you need to add another script, which is called from the above shutdown script. More details about this issue can be read in this forum post.
  • Key in
    sudo nano multi_switch.sh
  • Copy and paste the complete script from the github page of the above forum post from here into the nano editor
  • Press ctrl+x to exit, y to confirm, enter to save the file
  • Key in
    sudo chmod +x multi_switch.sh

The following additional script enables the RemotePi Board to cut off the power, after RetroPie has been shut down from the on-screen menu.

  • Key in
  • sudo nano /etc/shutdown.sh
  • Copy and paste the following contents
  • #!/bin/bash
    REBOOT_GREP=$(systemctl list-units --type=target | grep reboot)
    SHUTDOWN_GREP=$(systemctl list-units --type=target | grep shutdown)
    if [ "$SHUTDOWN_GREP" ] && [ ! "$REBOOT_GREP" ]; then
      # the condition above makes sure, that the shutdown sequence is not run on reboot 
      GPIOpin=15
      GPIOpin1=14
      echo "$GPIOpin" > /sys/class/gpio/export
      echo "out" > /sys/class/gpio/gpio$GPIOpin/direction
      echo "1" > /sys/class/gpio/gpio$GPIOpin/value
      sleep 0.125
      echo "0" > /sys/class/gpio/gpio$GPIOpin/value
      sleep 0.2
      echo "1" > /sys/class/gpio/gpio$GPIOpin/value
      sleep 0.4
      echo "0" > /sys/class/gpio/gpio$GPIOpin/value
      echo "$GPIOpin1" > /sys/class/gpio/export
      echo "out" > /sys/class/gpio/gpio$GPIOpin1/direction
      echo "1" > /sys/class/gpio/gpio$GPIOpin1/value
      sleep 4
    fi
  • Press ctrl+x to exit, y to confirm, enter to save the file
  • After saving the file, mark it as executable with
    sudo chmod +x /etc/shutdown.sh
  • Key in
  • sudo nano /etc/systemd/system/irswitch2.service
  • Copy and paste the following text into the editor window
    [Unit]
    Description=RemotePi Board Shutdown Service
    Before=multi-user.target
    Conflicts=shutdown.target
    [Service]
    ExecStart=/bin/true
    ExecStop=/etc/shutdown.sh
    Type=oneshot
    RemainAfterExit=yes
    [Install]
    WantedBy=multi-user.target
  • Press ctrl+x to exit, y to confirm, enter to save the file
  • Key in
    sudo systemctl enable irswitch2.service

  • Reboot one time
  • After reboot you can use the RemotePi Board to power cycle RetroPie

 

The above script has been tested and is confirmed to work up to RetroPie 4.5.1. Although not yet tested, it most probably will work with newer versions as well.

This page was last updated 23-Nov-2019