• Recent
  • Tags
  • Popular
  • Home
  • Docs
  • Register
  • Login
RetroPie forum home
  • Recent
  • Tags
  • Popular
  • Home
  • Docs
  • Register
  • Login

Multi Switch Shutdown Script!

Scheduled Pinned Locked Moved Ideas and Development
shutdown scriptshutdown switchcyperghost
272 Posts 40 Posters 109.3k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R
    Rion @mitu
    last edited by Rion 17 May 2019, 11:42

    @mitu Thank you Mr Bash Shell Wizard!

    FBNeo rom filtering
    Mame2003 Arcade Bezels
    Fba Arcade Bezels
    Fba NeoGeo Bezels

    1 Reply Last reply Reply Quote 0
    • R
      Ranma @mitu
      last edited by 17 May 2019, 17:16

      @mitu I'll add my love here too. This works! Awesome! Thanks so much for replying.

      You always know the answer @mitu

      Superstar!

      1 Reply Last reply Reply Quote 0
      • J
        janderclander14
        last edited by janderclander14 6 Aug 2019, 15:21 8 Jun 2019, 14:20

        I've modified the python script from this post, which calls the Multiswitch script, to add several reset behaviors with generic buttons connected to the GPIO.

        Assuming two buttons, the behavior is as follows:

        • One button (connected to GPIO 3) acts as shutdown (and power up)
        • The other button resets the system. One short press, quits the current emulator or restarts emulationstation. One long press (3 sec) restarts the system.

        I attach here the code just in case someone finds it useful (it assumes multi_switch.sh is in /home/pi). You may add this as a python call to /opt/retropie/configs/all/autostart.sh

        #!/usr/bin/env python3
        from gpiozero import Button, LED
        import os 
        from signal import pause
        import subprocess
        import time
        
        powerPin = 3 
        resetPin = 2 
        
        resetMinSeconds = 2
        
        #functions that handle button events
        def when_pressed():
         output = int(subprocess.check_output(['/home/pi/multi_switch.sh', '--es-pid']))
         
         if output:
             os.system("/home/pi/multi_switch.sh --es-poweroff")
         else:
             os.system("sudo shutdown -h now")
        
        def reboot():
         start_time=time.time()
         diff=0
        
         while rebootBtn.is_active and (diff < resetMinSeconds):
             now_time=time.time()
             diff=-start_time+now_time
        
         output = int(subprocess.check_output(['/home/pi/multi_switch.sh', '--es-pid']))
         output_rc = int(subprocess.check_output(['/home/pi/multi_switch.sh', '--rc-pid']))
         if (diff > resetMinSeconds):
             if output:
                 os.system("/home/pi/multi_switch.sh --es-reboot")
             else:
                 os.system("sudo reboot")
         else:
             if output_rc:
                 os.system("/home/pi/multi_switch.sh --closeemu")
             elif output:
                 os.system("/home/pi/multi_switch.sh --es-restart")
             else:
                 os.system("sudo reboot")
         
        btn = Button(powerPin)
        rebootBtn = Button(resetPin)
        rebootBtn.when_pressed = reboot 
        btn.when_pressed = when_pressed
        pause()
        
        C M 3 Replies Last reply 8 Jun 2019, 21:05 Reply Quote 3
        • C
          cyperghost @janderclander14
          last edited by 8 Jun 2019, 21:05

          @janderclander14 Hehe ;) Nicely done

          1 Reply Last reply Reply Quote 0
          • C
            cyperghost @janderclander14
            last edited by cyperghost 6 Sept 2019, 07:21 9 Jun 2019, 06:18

            @janderclander14 I've done some changes to the python script for some other system. To get rid of the pathes I added

            #get script directory
            scriptDir = os.path.dirname(os.path.realpath(__file__))
            

            So the directory from the python script is catched. Assuming the python and the bash script are stored in the same directory we can use....

            os.chdir(scriptDir)
            ..
            ..
                 elif output:
                     os.system("./multi_switch.sh --es-restart")
            

            I was fixed to to /opt/RetroFlag because RetroFlag use as default in the installer and so I use this path to overwrite their script. But congrats you are the first one who activly uses the provided output to create a new shutdown script. Such it was intended to do ;)

            J 1 Reply Last reply 9 Jun 2019, 20:02 Reply Quote 1
            • J
              janderclander14 @cyperghost
              last edited by 9 Jun 2019, 20:02

              @cyperghost Certainly, using relative directories is always better. And thanks to you for the script! It just had all the functionalities one would need.

              1 Reply Last reply Reply Quote 0
              • Q
                quicksilver
                last edited by 9 Aug 2019, 15:48

                @cyperghost Would it possible to adapt your script to work with the new RetroFlag GPi case? It only has a shutdown switch (no reset function). This is the script that is currently supplied by retroflag:

                #!/usr/bin/env python3
                from gpiozero import Button, LED
                import os 
                from signal import pause
                
                powerPin = 26 
                powerenPin = 27 
                hold = 1
                power = LED(powerenPin)
                power.on()
                
                #functions that handle button events
                def when_pressed():
                  os.system("sudo killall emulationstation && sleep 5s && sudo shutdown -h now")
                  
                btn = Button(powerPin, hold_time=hold)
                btn.when_pressed = when_pressed
                pause()
                

                As you can see they are just using a "sleep" function to handle saving metadata which is not ideal.

                C 1 Reply Last reply 9 Aug 2019, 18:46 Reply Quote 0
                • C
                  cyperghost @quicksilver
                  last edited by cyperghost 8 Sept 2019, 19:47 9 Aug 2019, 18:46

                  @quicksilver Yes .... the os.system call has to be changed.

                  os.system("bash /path/to/multi_switch/multi_switch.sh --es-poweroff")
                  

                  This will try to close all open emulators and then shutdown the system (after all metadata is saved)

                  Q 1 Reply Last reply 9 Aug 2019, 20:26 Reply Quote 1
                  • Q
                    quicksilver @cyperghost
                    last edited by 9 Aug 2019, 20:26

                    @cyperghost That was easy and it works like a charm! Thank you my friend!

                    1 Reply Last reply Reply Quote 0
                    • Q
                      quicksilver
                      last edited by 18 Aug 2019, 14:54

                      @cyperghost is there a way to get your script to shutdown if you exit emulation station to the terminal? Or if emulation station crashes or something?

                      C 1 Reply Last reply 18 Aug 2019, 18:28 Reply Quote 0
                      • C
                        cyperghost @quicksilver
                        last edited by 18 Aug 2019, 18:28

                        @quicksilver said in Multi Switch Shutdown Script!:

                        is there a way to get your script to shutdown if you exit emulation station to the terminal?

                        Therefore you can use .bashrc from your home directory.
                        Just add a poweroff command and if you go to terminal the command will be executed.

                        Or if emulation station crashes or something?

                        You would ask for the PID of Emulationstation simply with pidof emulationstation
                        You will obtain the one from the binary and as long this is alive you know ES is running. If there is a change in the PID or the status of SIGNAL has changed you can assume that there someting happend to ES. To determine if it a user action (Reboot, Shutdown...) you watch for files placed into/tmp dir

                        The files are

                        1. /tmp/es-sysrestart for sytem wide reboot
                        2. /tmp/es-restart for just restarting ES
                        3. /tmp/es-shutdown for poweroff

                        So to answer your question if my script can do this .... only for the .bashrc part that is really no rocket science and a sudo poweroff command call would be more effictive.

                        Be aware to not shoot in your own leg with this!

                        1 Reply Last reply Reply Quote 1
                        • WeirdHW
                          WeirdH
                          last edited by 20 Aug 2019, 14:03

                          Just wanted to say thanks for this awesome script. After (unsuccessfully) messing around with 'dtoverlay' in config.txt and some years old non-functioning auto-installed stuff, I finally have a single button safe shutdown for ES. Thanks to cyperghost and all involved!

                          1 Reply Last reply Reply Quote 2
                          • D
                            danick8989
                            last edited by 12 Sept 2019, 21:00

                            SuperPi case, i came from your outdated script that's linked on RetroFlag's github and this script doesn't work. When a game is running, i need to flip the reset button several times then it kills retroarch. Same for shutdown, after 2-3secs "Killed" appears on the screen then it shuts down

                            C 1 Reply Last reply 12 Sept 2019, 21:14 Reply Quote 0
                            • C
                              cyperghost @danick8989
                              last edited by 12 Sept 2019, 21:14

                              @danick8989 You to explain in a more clear way. The script can be utilzed in two ways.

                              1. If you use it in pure shell mode then you need to hold the button for one second. The pure bash script uses simple polling to check GPIO state.
                              2. If you "came" from RetroFlag then you utilize python and this uses interrupts so it recognizes button presses instantly. And the lack of "reaction" is likely a faulty case.

                              Furthermore: if it terminates retroarch and returns you to ES and it shuts down your Pie why are you assuming it's outdated and does not work?

                              D 1 Reply Last reply 13 Sept 2019, 03:41 Reply Quote 0
                              • D
                                danick8989 @cyperghost
                                last edited by danick8989 13 Sept 2019, 03:41

                                @cyperghost I'm not assuming it as outdated, i said i came from your previous revision of the script that's linked on RetroFlag's github page(they didn't update it to this one). And i thought killing retroarch and ES would be discouraged? Like, previously didn't the script send a command to ES for it to close RA properly or something? Also it's not clear to me, how do i use python instead of a bash script, and since it seems to work more seamlessly, why isn't it the default? I rolled back to the old multiswitch script in the meantime, since it still works flawlessly for me aside from exiting from Kodi.

                                1 Reply Last reply Reply Quote 0
                                • AndersHPA
                                  AndersHP
                                  last edited by AndersHP 10 Nov 2019, 11:46 11 Oct 2019, 10:44

                                  Hi, I kind of hijacked another thread regarding Pegasus, but thought I would ask here instead.

                                  I'm not using a RetroFlag unit, but a Kite Circuit Sword from Sudomod.com

                                  I'm unsure if my shutdown script, the cs_shutdown.sh file in /opt/ folder, does something special that can harm the safe-shutdown process if not built into another script, e.g. your Multi Switch Script. My file looks like this (also mentions a shutdown file?):

                                  #!/bin/bash
                                  
                                  # Get ES pid
                                  ES_PID=`pidof emulationstation`
                                  
                                  if [[ $ES_PID != "" ]] ; then
                                  
                                    echo "ES_PID: $ES_PID"
                                  
                                    # Touch the shutdown file
                                    touch /tmp/es-shutdown
                                    chown pi:pi /tmp/es-shutdown
                                  
                                    # Tell ES to terminate
                                    kill -s SIGTERM $ES_PID
                                  
                                    echo "ES has been asked to shutdown"
                                  
                                  else
                                  
                                    echo "Could not find ES PID ($ES_PID)"
                                    sudo shutdown -h now &
                                  
                                  fi
                                  

                                  Could I just delete this file, and install your Multi Switch Script with the modifications that @SinisterSpatula mentions? Or would I miss something? How much is this solely for RetroFlag builds?

                                  For instance: The RetroFlag is a Pi Zero right? My Circuit Sword is a CM3+ module (not GPIO based, but basically a RPi 3+).

                                  How can I tell if the script works as intended apart from the screen going black?

                                  My "Bubble Bobble" Themed Bartop Arcade
                                  My Gameboy

                                  mituM 1 Reply Last reply 11 Oct 2019, 11:26 Reply Quote 0
                                  • mituM
                                    mitu Global Moderator @AndersHP
                                    last edited by 11 Oct 2019, 11:26

                                    @AndersHP Try replacing

                                    ES_PID=`pidof emulationstation`
                                    

                                    with

                                    ES_PID=`pidof pegasus-fe`
                                    

                                    or what's the actual process name of the Pegasus front-end. The rest of the script should be the same.

                                    AndersHPA 1 Reply Last reply 11 Oct 2019, 12:06 Reply Quote 0
                                    • AndersHPA
                                      AndersHP @mitu
                                      last edited by AndersHP 10 Nov 2019, 13:07 11 Oct 2019, 12:06

                                      @mitu OK thanks, but can I see somehow if everything is working as planned - apart from the obvious thing that Pegasus should quit?

                                      My "Bubble Bobble" Themed Bartop Arcade
                                      My Gameboy

                                      mituM 1 Reply Last reply 11 Oct 2019, 12:08 Reply Quote 0
                                      • mituM
                                        mitu Global Moderator @AndersHP
                                        last edited by 11 Oct 2019, 12:08

                                        @AndersHP The script you posted does nothing more than stopping Pegasus and shutting down, so if comment out the shuttind down part you can check by seeing if the front-end stops.

                                        1 Reply Last reply Reply Quote 1
                                        • AndersHPA
                                          AndersHP
                                          last edited by 11 Oct 2019, 18:37

                                          Did the pidof pegasus-fe trick and pegasus quits to terminal now.
                                          But the machine doesn't power off now...?

                                          My "Bubble Bobble" Themed Bartop Arcade
                                          My Gameboy

                                          C 1 Reply Last reply 11 Oct 2019, 19:14 Reply Quote 0
                                          • First post
                                            Last post

                                          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.

                                            This community forum collects and processes your personal information.
                                            consent.not_received