Please do not post a support request without first reading and following the advice in

Multi Switch Shutdown Script!

  • Hallo,

    I got some time and was able to code a swift script, that helps you to perform save shutdowns to your RetroPie installation. So you can see this script as the swiss army knife for ES save shutdowns that will outstand much others. It's very versatile and easy to implent in any coding language that provides readout of stdout feature (python, C, BASIC, bash...)

    This script supports ALL cases that werte produced by RetroFlag. So the parameter set to use for

    • NESPi+
    • SuperPi
    • MegaPi

    cases is just the --NESPI+ switch. Detailed instructions to install multi_switch script to this dedicates cases are located here

    DOWNLOAD: get it on github

    Tired on bash scripts? Get a functional shutdown for your NESPi+ on python (posting 2)*

    *This is based on the original script proided by retro-flag, but has some improvements


    Every Parameter gots it's own helppage now:

    • type ./ without arguments will display just a short help page
    • type ./ -h brings an extended help page with all arguments online
    • type ./ --COMMAND help will display the specific helppage for the argument or command given
    • remember you can set now GPIOs via commandline, no need to edit the script manuall. Read more on the infopage ... for ex. ./ --nespi+ help

    Detailed instructions for install

    What is save shutdown?
    ES got the habit to not save metadata if it is not proper shutdowned. If you just perform a system reboot via SSH command or type killall emulationstation then indeed ES is closed but no metadata is saved. Metadata are those XML files that are used to store scraped data AND since ES 2.4.1 to save favorites and last played games. So it is important to have "Save metadata on exit" enabeled and to perform proper shutdowns.

    What is metadata?
    In short everything that has something to do with gameslists.xml per system. So it's scraped data and since @pjft introduced the favourites/last playes/custom collections-system into ES ... these are also affected. So you will never loose any metadata as these scripts provide proper shutdowns ;)

    Furthermore this script can handle save shutdown for/via:

    5 Common power devices or Cases for our Raspberry

    • Generic Button, momentary or latching (supports power off with all metadata saved)
    • MausBerry (supports power off with all metadata saved)
    • Pimoroni OnOff SHIM (supports power off with all metadata saved)
    • POLOLU Switch in NESPi case (Yahmez-MOD) (supports reset* button and power off with all metadata saved)
    • NESPi+ Case by Retroflag (supports fan control, reset* button and power off with all metadata saved)

    * reset button: If an emulator is running and you press RESET button, you will be kicked back to EmulationStation MainScreen! If you press RESET button in ES MainScreen, ES will be reloaded (no system reboot) - you can change this behaviour also if you want by editing the script.

    You can also download script from my GitHub account

    Support for OSMC

    Support for OSMC is given by some modifications done to the shutdown script.
    @denisuu made great efforts in doing this. By following his instructions here you will be able to get proper shutdown with any Retroflag case together with OSMC operating system.


    1. Login with SSH
    2. Type in commands mkdir /home/pi/RetroPie/scripts && cd /home/pi/RetroPie/scripts
    3. DL: wget && chmod +x
      3.1 If there is an older version please remove this one with rm
      3.2 Otherwise the current downloaded version will get a .sh.1 filename!
    4. Now edit ES autostart with nano /opt/retropie/configs/all/ and add script to like ....
    5. /home/pi/RetroPie/scripts/ --nespi+ & but BEFORE the last line emulationstatio #auto
      5.1 Use suitable parameter sets --nespicase &, --nespi+ &, --generic &, --onoffshim &, --mausberry &
      5.2 Generic Button, NESPiCase @Yahmez -Mod and NESPi Case + makes use of internal PullUp Resistors, therefore raspi-gpio is needed
      5.3 Install raspi-gpio with sudo apt install raspi-gpio (only for Generic Button, NESPiCase and Yahmez-Mod needed!)
      5.4 the sudo commands depends of usecase, Mausberry and OnOffShim needs it for GPIO export, both NESPiCase(+) and Generic Button not
    6. Give me some feedback ;)

    SetUp examples

    Python Code

    Example to invoke the script into PYTHON-CODE? I've written a small tutorial here!

    Command Line Parameters


    • --es-pid Shows PID of ES, if not it shows 0
    • --rc-pid Shows PID of - shows 0 if not found
    • --es-systemd Hook for the famous "Gracefully exit with metadata saved"-Service by @meleu
    • --es-closeemu Tries to shutdown emulators, with cyperghost method
    • --es-poweroff Shutdown emulators (if running), Closes ES, performs poweroff
    • --es-reboot Shutdown emulators, Cloese ES, performs system reboot
    • --es-restart Shutdown emulators (if running), Restart ES


    • --mausberry If you have a Mausberry device, GPIO 23 24 used!
    • --onoffshim If you have the Pimoroni OnOff SHIM GPIO 17 and 4 used!
    • --nespicase If you use the NESPICASE with yahmez-mod GPIO 23 24 25 used!
    • --nespi+ If you use the NESPI+ CASE original from RetroFlag GPIO 2 3 4 14 used!
    • --generic You can use latching and momentary button for this connected to any GPIO and common ground, default is GPIO 3 as only this provides powerdown and repower ability

    Power Switch devices and cases

    Example of setting up devices:

    • The GENERIC PUSH BUTTON can be run simple with ./ --generic, think about to install raspi-gpio, too!
    • The NESPICASE can be run simple with ./ --nespicase, this used raspi-gpio, too!
    • The NESPI+ CASE can be run just with ./ --nespi+ please read more about here reagarding shutdown of the fan and other external devices. This uses also the raspi-gpio binary to control status of GPIO pins.
    • The MAUSBERRY needs sudo privileges so run with sudo ./ --mausberry
    • Same with the ONOFFSHIM of Pimoroni sudo ./ --onoffshim but be aware for this device you need a systemd call also! Read the TUTORIAL HERE and follow steps!

    Setup your GPIOs via commandline

    Up to now 4 parameters are supported

    1. powerbtn= with this command you set desired GPIO the powerbutton is attached to. If you left unsigned or you enter wrong setting, then default values are used. All devices support that command
    2. resetbtn= with this command you set desired GPIO the resetbutton is attached to. If you left unsigned or you enter wrong setting, then default values are used. Only the both NESPi cases supports that type. So if you use this command on a generic button it will be ignored!
    3. powerctrl= with this command you set desired GPIO the power ON control is attached to. All devices needs this, except the generic button only! If you enter wrong values or leave it blank the default values for expected device are used. This command indicates the power device in which state the Raspberry is, so a complete power cut can be performed!
    4. ledctrl= with this command you set desired GPIO a LED can be shut ON or OFF. Up to now only the NESPi cases supports that feature. I think I can integrate it to other devices, too.

    So you set for example your Mausberry-switch via commandline:

    1. default values: --mausberry this will use GPIO23 for power button and GPIO24 for power ON control.
    2. --mausberry powerbtn=17 this will use GPIO17 for power button and GPIO24 for power ON control still as default
    3. --mausberry powerctrl=4 powerbtn=3 this will use GPIO3 for power button and GPIO4 for power ON control
    4. --mausberry powerbutton=3 powerctrl=3a will use default values GPIO23 for power button and GPIO24 for power ON control as parameters were all setted wrong.

    Created version 0.85

    Big leaps are done now:

    • v0.70 Parameter control, added extended help pages
    • v0.75 Parameter --CLOSEEMU is called --ES-CLOSEEMU (both can be used for backward compatibility!)
    • v0.80 Introduced --ES-SYSTEMD parameter, now the ES gracefully shutdown service by @meleu can be used
    • v0.85 Code cleanup, added watchdog to kill only persistent emulators with sig -9, added more helppages

    Created version 0.51

    Finally the NESPi+ case can completly powerdown!
    Please install systemd service like done in Pimoroni OnOffShim!
    cd /lib/systemd/system-shutdown/ && sudo wget && sudo chmod +x shutdown_fan

    Created version 0.50

    Added Generic Button on GPIO3
    Use a latching or momentary button connected to any GPIO and common ground to powerdown system. Default is GPIO3 as only this pin provides poweroff and repower ability!

    Created version 0.42

    Added NESPi+ Case fan control!
    100% supported now!
    Thx to @gollumer @cloudlink

    Created version 0.40

    Added NESPi+ Case from retroflag!

    Created version 0.32

    Added check for user privileges and package depencies - thanks @Semper-5 for feedback

    Here is my small contribution for some people out here. This script supports

    • NESPiCase (hope Yahmez can test)
    • MausBerry device (original script, modified for safe shutdown
    • Pimoroni OnOff Shim (script modified by me)

    All scripts support safe shutdown via power button and full detection of running emulators. So you hopefully will never loose save progress.

    Created version 0.30

    We can now use command-line options

  • @cyperghost I like the logic a lot. The way I see it, we have learned enough to make a versatile and very capable solution. I am thinking the best is one that will adapt to everyone's use cases. All of the actions/components I have seen that we need to deal with are:

    1. A poweroff button (GPIO trigger, momentary and latching, often power circuit involved)
    2. A reset button (GPIO trigger, sometimes also part of the power circuit)
    3. Restarting an Emulator action
    4. Shutting down emulator(s) action
    5. Shutting down ES (in a way that saves metadata) action
    6. Powering OFF the Pi action
    7. Restarting the Pi action

    Each of us will have a different idea about what they want to achieve, but keeping the scripts modular might give folks the most flexibility. For example, I want 1 and 2 above to be Python scripts so I can handle GPIO edge detection efficiently.

    On one of my systems, all I have is a momentary power button (wired to mausberry), so I want my setup to perform 1,4,5,6. But on my Nespi case, I also have a reset, so I want one button to follow 1,4,5,6 and another that does 2,4,5 only, (or even better, an optional 2,3, and 2,4,5 if held down).

    Does the script you shared above perform #4?

  • You could have a single script as the base and comment out the unneeded lines...

    I do like python for GPIO calls...

  • Global Moderator


    Shutting down the emulators was always gonna be the tricky part. Your ScummVM example was one I wouldn't have known how to deal with. The solution you came up with looks like it should cover most, if not all cases. Nice job!

  • The emulator currently kicking my rear is the entire N64 set

  • So, I am going to try and use this script with my GPIO power button script...

    On my cell but the goal will be

    -bottom push detection
    —possibly find a way for user input to choose the GPIO used
    -initiate emulator kill script
    -wait til all emulators are closed
    -close emulation station saving metadata
    -shutdown system

    Aiming to use python for most of it...

  • @mediamogul said in Shutdown Script - Some input for better emulator detection:


    Shutting down the emulators was always gonna be the tricky part. Your ScummVM example was one I wouldn't have known how to deal with. The solution you came up with looks like it should cover most, if not all cases. Nice job!

    This isn't a new case indeed. It took some days to find out a solution. You can read more about here: Mausberry shutdown script doesn't save metadata and this script here as the "mother" of all shutdown scripts.

    So it was a real hard job to get the "SCUMMVM"-fix with a pkill -P request idea introduced be me.... But the whole RegExing was done by meleu. Later he thankfully created the shutdown service ;)

    The pkill method lacks of working if there would be annother call to executable for example and with the pid-array we would bypass to read out log-files. So we avoid the sed command to extract emulator call. I think this is a more "straight" way.

    The main idea is following. Even if you use programms that won't be called by like quake darkplaces.... We can use a call like BYPASS "darkplaces -config....."and would also get the PID as runcommand calls this file as seconds shell ;) Do you understand my intention? But this is a task for BuZz and I don't know if he likes the idea ;)

    This script provides usecase 4
    It shutdown emulators. I've written a script for my NESPi case to... You can take a look here: Yet annother NESPI case. Just disable the DUOLED part and you can switch off (even with software shutdown) the Mausberry. Reset works: If an emulator is running it will terminate this. If you are in ES mainmenu and press Reset then, ES will be restarted (more usefull than a reboot imho)

    Yes, a general usecase script would be fine. But it isn't as easy.... There are tons of devices out there

  • @cyperghost that is true... I am unfimilar with power block and such...

    In regards to NESPi, doesn’t it connect from the button to the GPIO?

  • @drakaen391 The NESPi case needs some modifications. caver01 and me used a MAUSBERRY so we cutted traces to isolate the switches. Those seperated switches were connected to GPIO.

    From here events can be handled ... power button, reset button. It's a bit of mess and every user has his own "flavour" how button presses are handeld.

    Personally I prefer bash over python because bash is more powerfull for process handling.
    python got's the advantage with the interrupt processes (which are more accurate to handle inputs) but even there are some bash tools to use interrupt sessions.

    99% of bash scripts (shutdown scripts) use polling. They use a sleep timer to check button status in intervalls. I don't know if this is a big loose in performance but I prefer one switch-script that can handle all cases instead of a python script for button handling, a bash script for safe shutdown, annother bash script for ending emulators ....

  • Hi @cyperghost , I'm testing this script and works perfectly. Many thanks for the great job!!


  • @cyperghost does your script require raspi-GPIO on standard cases?

  • Hi @cyperghost , I combine a modified shutdown python script (reboot/shutdown with the same button by time, power on led of the button and video playback for reboot/shutdown actions) with your v0.07 of this script and works for me perfectly. It would be better if ES would not show up when killing the emulator process but it works and save metadata. This method would be totally generic!! Have you ever thought about doing something with the same characteristics?
    Another question out of curiosity... whe your script kills emulator process ES is showed in the screen before closing during one or two seconds more or less?


  • @cyperghost my plan is to create a python script for the GPIO but use either .ini or .cfg for people to customize

    It will look sorta like...

    GPIO triggered

    Check config

    Execute per selection

  • @Drakaen391
    That would be a good progress imho.
    You can use the same logic out from the bash script.
    Let me do some changes and I can provide a script that has several command line parameters.

    Command Line Parameters

    • --es reboot/shutdown/restart
    • --emuclose

    About the raspi-gpio
    It's not a question of the case you insert the Pie. It's more like how the GPIOs are triggered. If you read the internet in 99% of all howtos people use 3.3V and a resistor to input current to the GPIO. So of you press the button the current flow from ~3.3V is shorted to ground over an resistor.

    That's a comon method for the Mausberry switch for example. And you see it here in my script:

    echo "$GPIO_powerswitch" > /sys/class/gpio/export
    echo "in" > /sys/class/gpio/gpio$GPIO_powerswitch/direction

    @Yahmez used a clever way to solve that issue. He made use of the internal Pullup resistors. They work exactly same as an external (internal I think are 50kOhm resistors) but you need only a switch that is connected to a GPIO with activated internal resistors and connect it to ground, so outside no 3.3V rail is needed and all looks clean.

    Sadly bash isn't (easy) capable to export those function as filesystem. So you have no easy access to it. That's the reason raspi-gpio comes in. This C written tools sets the internal pullups and gives message out. So I just compare piped grep ouput if there is some change.
    There are also lot's more of that tools.

    1. PIGPIO (very capable but need to load it as service)
    2. wiringPI (very strong but I don't like the naming style of the GPIOs as they are copied from ARDUINO)
    3. at last raspi-gpio.
    4. surly others

    If you ask ... bash needs those extra programs and python has it all integrated then you are wrong. Python needs also a "external" programm, but it is called "LIBARY" und has to be imported.

    Just delete/comment the specific echo and sleep commands. Then all will be fine.

  • @cyperghost Sorry, maybe I explained wrong ... what I would like to know is if when your script kills the emulator process and is ready to close emulationstation the emulationstation menu is displayed for 1 or 2 seconds and then closes saving metadata ... Sorry for the bad explanation. I did not have the opportunity to try meleu's script when that method still worked and what I have now in my Pi works that way, it is not very important but I would like it more if it doesn't do it.


  • @cyperghost then I think using Python for the GPIO call would be easier because it doesn’t require any extra installs (the libraries are preinstalled)

    I will write something up later after I do some errands

  • @drakaen391 I think I can implent function calls.
    So you use python to setup triggers and make a call to script like --es poweroff
    This will shutdown ES, closes all emulators running and saves metadata.
    Then poweroff command will be performed. --espid
    gives back PID from ES....

    I made emulator shutdown and ES shutdown as quick as possible.
    I wait for closing and then close ES.
    I think the script from meleu uses a static timer after emulator shutdown so you see the ES screen for a few seconds.

    You can post your current script to pastebin and write link here.

  • Created version 0.30

    We can now use command-line options
    @Drakaen391 that's the way you want it :D


    • --es-pid Shows PID of ES, if not it shows 0
    • --rc-pid Shows PID of - shows 0 if not found
    • --closeemu Tries to shutdown emulators, with cyperghost method
    • --es-poweroff Shutdown emulators (if running), Closes ES, performs poweroff
    • --es-reboot Shutdown emulators, Cloese ES, performs system reboot
    • --es-restart Shutdown emulators (if running), Restart ES


    • --mausberry If you have a Mausberry device, GPIO 23 24 used!
    • --onoffshim If you have the Pimoroni OnOff SHIM GPIO 17 and 4 used!
    • --nespicase If you use the NESPICASE with yahmez-mod GPIO 23 24 25 used!

    Please update code form my

    GitHub account

  • @cyperghost oh, yes... I have no complaints about your script, it is perfect, it closes all the processes very fast ... the problem is that I want to have implemented the functions of restart and shutdown in a single button and the possibility of turning on a LED for buttons with integrated LED. For this reason (and because I do not know how to program bash scripts) I have implemented only the part of kill emulator process of your script (calling the bash script) inside a Python script that performs the rest of functions, but it takes like a second or two to close emulationstation. If you are curious, I can upload the code so you can see it. I will try implement in the same code another gpio only for shutdown in cases of low battery in gameboy zero projects for example

Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.

Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.