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 125.4k 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.
    • quicksilverQ
      quicksilver
      last edited by

      @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.

      cyperghostC 1 Reply Last reply Reply Quote 0
      • cyperghostC
        cyperghost @quicksilver
        last edited by cyperghost

        @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)

        quicksilverQ 1 Reply Last reply Reply Quote 1
        • quicksilverQ
          quicksilver @cyperghost
          last edited by

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

          1 Reply Last reply Reply Quote 0
          • quicksilverQ
            quicksilver
            last edited by

            @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?

            cyperghostC 1 Reply Last reply Reply Quote 0
            • cyperghostC
              cyperghost @quicksilver
              last edited by

              @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

                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

                  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

                  cyperghostC 1 Reply Last reply Reply Quote 0
                  • cyperghostC
                    cyperghost @danick8989
                    last edited by

                    @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 Reply Quote 0
                    • D
                      danick8989 @cyperghost
                      last edited by danick8989

                      @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

                        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 Reply Quote 0
                        • mituM
                          mitu Global Moderator @AndersHP
                          last edited by

                          @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 Reply Quote 0
                          • AndersHPA
                            AndersHP @mitu
                            last edited by AndersHP

                            @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 Reply Quote 0
                            • mituM
                              mitu Global Moderator @AndersHP
                              last edited by

                              @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

                                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

                                cyperghostC 1 Reply Last reply Reply Quote 0
                                • cyperghostC
                                  cyperghost @AndersHP
                                  last edited by cyperghost

                                  @AndersHP your script will just close Pegasus and that's all. you should add a shutdown command to completly shutdown your system.

                                  your (easy) script would more look like...

                                  #!/bin/bash
                                  PEGASUS_BIN="pegasus-fe"
                                  if [[ -n $(pidof $PEGASUS_BIN) ]] ; then
                                      killall -w $PEGASUS_BIN
                                  fi
                                  sudo poweroff
                                  

                                  Much cleaner and more generic than the original one.

                                  If you want the proper exit of all emulators then use this script...

                                  #!/bin/bash
                                  PEGASUS_BIN="pegasus-fe"
                                  if [[ -n $(pidof $PEGASUS_BIN) ]] ; then
                                      bash multi_switch.sh --closeemu
                                      killall -w $PEGASUS_BIN
                                  fi
                                  sudo poweroff
                                  1 Reply Last reply Reply Quote 0
                                  • AndersHPA
                                    AndersHP
                                    last edited by

                                    But the script literally powered off my Gameboy before I did the ES -> Pegasus change...?

                                    My "Bubble Bobble" Themed Bartop Arcade
                                    My Gameboy

                                    cyperghostC 1 Reply Last reply Reply Quote 0
                                    • cyperghostC
                                      cyperghost @AndersHP
                                      last edited by

                                      @AndersHP But this script does behave like your original one. If you are in terminal and type sudo poweroff does then your mashine power off completly?

                                      The difference between ES and Pegasus is, that ES uses a "helper" script for shutdown status, therefore the touch-thing.

                                      I think you should be more specific for your setup, which additional scripts are running, how did you install them.....

                                      1 Reply Last reply Reply Quote 1
                                      • AndersHPA
                                        AndersHP
                                        last edited by

                                        I changed to the above mentioned script, and it does quit and then shutdown. Thanks for the help - will try the Multi Switch script next.

                                        My "Bubble Bobble" Themed Bartop Arcade
                                        My Gameboy

                                        cyperghostC 1 Reply Last reply Reply Quote 0
                                        • cyperghostC
                                          cyperghost @AndersHP
                                          last edited by cyperghost

                                          @AndersHP Don't forget to setup correct path to to script if you call bash multi_switch --closeemu - I know it's sometimes hard to remember doing the things correctly :)

                                          1 Reply Last reply Reply Quote 0
                                          • quicksilverQ
                                            quicksilver
                                            last edited by quicksilver

                                            @cyperghost Id like to use your multi shotdown script with my new argon one case because their script is much less functional than yours. Their argononed.py script looks like this:

                                            import smbus
                                            import RPi.GPIO as GPIO
                                            import os
                                            import time
                                            from threading import Thread
                                            rev = GPIO.RPI_REVISION
                                            if rev == 2 or rev == 3:
                                            	bus = smbus.SMBus(1)
                                            else:
                                            	bus = smbus.SMBus(0)
                                            GPIO.setwarnings(False)
                                            GPIO.setmode(GPIO.BCM)
                                            shutdown_pin=4
                                            GPIO.setup(shutdown_pin, GPIO.IN,  pull_up_down=GPIO.PUD_DOWN)
                                            def shutdown_check():
                                            	while True:
                                            		pulsetime = 1
                                            		GPIO.wait_for_edge(shutdown_pin, GPIO.RISING)
                                            		time.sleep(0.01)
                                            		while GPIO.input(shutdown_pin) == GPIO.HIGH:
                                            			time.sleep(0.01)
                                            			pulsetime += 1
                                            		if pulsetime >=2 and pulsetime <=3:
                                            			os.system("reboot")
                                            		elif pulsetime >=4 and pulsetime <=5:
                                            			os.system("shutdown now -h")
                                            def get_fanspeed(tempval, configlist):
                                            	for curconfig in configlist:
                                            		curpair = curconfig.split("=")
                                            		tempcfg = float(curpair[0])
                                            		fancfg = int(float(curpair[1]))
                                            		if tempval >= tempcfg:
                                            			return fancfg
                                            	return 0
                                            def load_config(fname):
                                            	newconfig = []
                                            	try:
                                            		with open(fname, "r") as fp:
                                            			for curline in fp:
                                            				if not curline:
                                            					continue
                                            				tmpline = curline.strip()
                                            				if not tmpline:
                                            					continue
                                            				if tmpline[0] == "#":
                                            					continue
                                            				tmppair = tmpline.split("=")
                                            				if len(tmppair) != 2:
                                            					continue
                                            				tempval = 0
                                            				fanval = 0
                                            				try:
                                            					tempval = float(tmppair[0])
                                            					if tempval < 0 or tempval > 100:
                                            						continue
                                            				except:
                                            					continue
                                            				try:
                                            					fanval = int(float(tmppair[1]))
                                            					if fanval < 0 or fanval > 100:
                                            						continue
                                            				except:
                                            					continue
                                            				newconfig.append( "{:5.1f}={}".format(tempval,fanval))
                                            		if len(newconfig) > 0:
                                            			newconfig.sort(reverse=True)
                                            	except:
                                            		return []
                                            	return newconfig
                                            def temp_check():
                                            	fanconfig = ["65=100", "60=55", "55=10"]
                                            	tmpconfig = load_config("/etc/argononed.conf")
                                            	if len(tmpconfig) > 0:
                                            		fanconfig = tmpconfig
                                            	address=0x1a
                                            	prevblock=0
                                            	while True:
                                            		temp = os.popen("vcgencmd measure_temp").readline()
                                            		temp = temp.replace("temp=","")
                                            		val = float(temp.replace("'C",""))
                                            		block = get_fanspeed(val, fanconfig)
                                            		if block < prevblock:
                                            			time.sleep(30)
                                            		prevblock = block
                                            		try:
                                            			bus.write_byte(address,block)
                                            		except IOError:
                                            			temp=""
                                            		time.sleep(30)
                                            try:
                                            	t1 = Thread(target = shutdown_check)
                                            	t2 = Thread(target = temp_check)
                                            	t1.start()
                                            	t2.start()
                                            except:
                                            	t1.stop()
                                            	t2.stop()
                                            	GPIO.cleanup()
                                            

                                            I tried changing the os.system to os.system("bash /path/to/multi_switch/multi_switch.sh --es-poweroff")like you suggested an earlier time that you helped me but then the reboot and shutdown dont seem to work anymore (I did use the correct path). Obviously I am missing something. I appreciate any guidance you could provide.

                                            cyperghostC 1 Reply Last reply Reply Quote 1
                                            • 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.