• 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.6k 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.
  • C
    cyperghost @mediamogul
    last edited by cyperghost 20 Apr 2018, 04:35

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

    @cyperghost

    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 runcommand.sh like quake darkplaces.... We can use a call like runcommand.sh 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 ;)

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

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

    D 1 Reply Last reply 20 Apr 2018, 13:25 Reply Quote 0
    • D
      Drakaen391 @cyperghost
      last edited by 20 Apr 2018, 13:25

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

      RPi B & RPi 3B OC (Now Raspberry pi 4b 8gb)
      Retropie (Latest Stable)
      PiBox with Wind Tunnel Cooling System

      C 1 Reply Last reply 20 Apr 2018, 13:34 Reply Quote 0
      • C
        cyperghost @Drakaen391
        last edited by cyperghost 20 Apr 2018, 13:34

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

        1 Reply Last reply Reply Quote 0
        • J
          julenvitoria Banned
          last edited by 20 Apr 2018, 21:39

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

          1 Reply Last reply Reply Quote 1
          • C
            cyperghost
            last edited by cyperghost 21 Apr 2018, 18:27

            OLD VERSION-- OUTDATED!

            D 2 Replies Last reply 21 Apr 2018, 22:47 Reply Quote 2
            • D
              Drakaen391 @cyperghost
              last edited by 21 Apr 2018, 22:47

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

              RPi B & RPi 3B OC (Now Raspberry pi 4b 8gb)
              Retropie (Latest Stable)
              PiBox with Wind Tunnel Cooling System

              1 Reply Last reply Reply Quote 0
              • J
                julenvitoria Banned
                last edited by julenvitoria 21 Apr 2018, 23:20

                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?

                Thanks!!

                1 Reply Last reply Reply Quote 0
                • D
                  Drakaen391 @cyperghost
                  last edited by 21 Apr 2018, 23:32

                  @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

                  RPi B & RPi 3B OC (Now Raspberry pi 4b 8gb)
                  Retropie (Latest Stable)
                  PiBox with Wind Tunnel Cooling System

                  C 1 Reply Last reply 22 Apr 2018, 05:57 Reply Quote 0
                  • C
                    cyperghost @Drakaen391
                    last edited by cyperghost 22 Apr 2018, 05:57

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

                    WIP:
                    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:

                    #Mausberry
                    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.

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

                    D 1 Reply Last reply 22 Apr 2018, 14:31 Reply Quote 0
                    • J
                      julenvitoria Banned
                      last edited by julenvitoria 22 Apr 2018, 10:47

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

                      Thanks

                      1 Reply Last reply Reply Quote 0
                      • D
                        Drakaen391 @cyperghost
                        last edited by 22 Apr 2018, 14:31

                        @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

                        RPi B & RPi 3B OC (Now Raspberry pi 4b 8gb)
                        Retropie (Latest Stable)
                        PiBox with Wind Tunnel Cooling System

                        C 1 Reply Last reply 22 Apr 2018, 14:49 Reply Quote 0
                        • C
                          cyperghost @Drakaen391
                          last edited by cyperghost 22 Apr 2018, 14:49

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

                          multi_switch.sh --espid
                          gives back PID from ES....

                          @julenvitoria
                          I made emulator shutdown and ES shutdown as quick as possible.
                          I wait for runcommand.sh 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.

                          C J 2 Replies Last reply 22 Apr 2018, 15:48 Reply Quote 0
                          • C
                            cyperghost @cyperghost
                            last edited by cyperghost 22 Apr 2018, 15:48

                            Created version 0.30

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


                            Systemcommand:

                            • --es-pid Shows PID of ES, if not it shows 0
                            • --rc-pid Shows PID of runcommand.sh - 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

                            SwitchDevices:

                            • --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

                            1 Reply Last reply Reply Quote 0
                            • J
                              julenvitoria Banned @cyperghost
                              last edited by julenvitoria 23 Apr 2018, 15:29

                              @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

                              C 2 Replies Last reply 23 Apr 2018, 15:35 Reply Quote 0
                              • C
                                cyperghost @julenvitoria
                                last edited by cyperghost 23 Apr 2018, 15:35

                                @julenvitoria That's also not the problem
                                You should use timer events. So you press the button (power button) if you release witihin 1.5 seconds you kill the emulators. If you don't realase the system power offs. Also doable ;) To led a LED glow

                                use raspi-gpio set GPIONrLEDCONNECTED op dh

                                to dim LED
                                use raspi-gpio set GPIONrLEDCONNECTED op dl

                                You can also export the port via bash command

                                echo "$GPIO_poweronctrl" > /sys/class/gpio/export
                                echo "out" > /sys/class/gpio/gpio$GPIO_poweronctrl/direction
                                echo "1" > /sys/class/gpio/gpio$GPIO_poweronctrl/value

                                You don't need to implent the code.... Use your python code and if power is going to suspend then use ./multi_switch.sh --es-poweroff command ;)
                                It's a kind of swiss army knife.

                                So it's up to you and your coding skills, to call this script with the commands it provides. The rest ... button timing, LED glowing is a question of your coding skills. My knowledge in python is very bare and I'm also a beginner in bash.

                                To deal with the output of this script use this python sniplet

                                import subprocess
                                
                                def run(command):
                                    output = subprocess.check_output(command, shell=True)
                                    return output
                                
                                C 1 Reply Last reply 24 Apr 2018, 15:44 Reply Quote 0
                                • C
                                  cyperghost @cyperghost
                                  last edited by cyperghost 24 Apr 2018, 15:44

                                  Created version 0.40

                                  Added NESPi+ Case!


                                  -- ORIGINAL FIRST POST! -- OUTDATED!! -- KEPT FOR NOSTALGICA


                                  I've lack of time so here is a base version of a script to detect PIDs of running emulator.


                                  This is the are bare (but) working sceleton of how emulators can be detected!

                                  You can download the script also from my github account: GITHUB - RETROPIE SHARES

                                  How does it work:

                                  1. This script uses runcommand.sh process as entrypoint
                                  2. It generates an array of child-PIDs called through runcommand.sh
                                  3. It shifts array backwards
                                  4. It sends TERM signal to all PIDs called by runcommand.sh (from newest to oldest)
                                  5. It waits if PID is finished
                                  6. Emulator is terminated

                                  So in practice:
                                  runcommand.sh [PID 1234] calls emulator lr-xxxxxx [PID 1236]
                                  So PID 1236 is terminated

                                  sometimes there are several script called
                                  runcommand.sh [PID 1234] calls bash file SCUMMVM [PID 1236] this calls SCUMMVM framework [PID 1345]

                                  So we TERM 1345 and wait for closing, TERM 1236 and wait for closing!

                                  The advantage here is... there is no need to hook /dev/shm/runcommand.log and read out used emulator. We need just an emulator call via runcommand.sh ;)

                                  @meleu @mediamogul @Drakaen391 @caver01
                                  Let us improve the shutdown scripts ;)

                                  #!/bin/bash
                                  # Shutdown Script Emulator Sceleton
                                  # by cyperghost for retropie
                                  # --------------------------
                                  # Initial release: ??? Don't know January 2018?
                                  # 04/19/18 Release in RetroPie forums
                                  # 04/20/18 julenvitoria introduced kill -9 signal for emulators
                                  # This function is called still all childPIDs are found
                                  function getcpid() {
                                  local cpids="$(pgrep -P $1)"
                                  for cpid in $cpids;
                                  do
                                  pidarray+=($cpid)
                                  getcpid $cpid
                                  done
                                  }
                                  # Abolish sleep timer! This one is much better!
                                  function smart_wait() {
                                  local PID=$1
                                  while [[ -e /proc/$PID ]]
                                  do
                                  sleep 0.05
                                  done
                                  }
                                  # Our entrypoint! RUNCOMMAND is alpha, emulator is OMEGA
                                  motherpid="$(pgrep -f -n runcommand.sh)"
                                  # if there is no runcommand.sh running then exit
                                  [ "$motherpid" ] && getcpid $motherpid || exit
                                  # Reverse array and do your TERM job
                                  for ((z=${#pidarray[*]}-1; z>-1; z--)); do
                                  echo "Terminate PID ${pidarray[z]}"
                                  kill -9 ${pidarray[z]}
                                  smart_wait ${pidarray[z]}
                                  done
                                  1 Reply Last reply Reply Quote 2
                                  • C
                                    cyperghost @julenvitoria
                                    last edited by 27 Apr 2018, 08:01

                                    @julenvitoria I implented the python script from the NESPi+ to work with my script. Works like a charm - python is very powerfull together with bash.

                                    So here is a full working example of how to integreate...
                                    For explaination how this works.
                                    The python script waits for a button event (the GPIOZERO Libery does this all in the background). A button is triggered now I as multi_switch.sh to output the ES-PID (to check if ES is running or not). If the result is a integer greater than 0 than it's a boolean true and we can shutdown ES with `./multi_switch --es-poweroff´ command (it closes running emulators, exits ES and initiates a shutdown) so all metadata is saved.

                                    If the answer is 0 we've got a boolean false and we use the regular shutdown.

                                    J 2 Replies Last reply 28 Apr 2018, 11:40 Reply Quote 0
                                    • J
                                      julenvitoria Banned @cyperghost
                                      last edited by julenvitoria 28 Apr 2018, 11:40

                                      @cyperghost Hi! Sorry but I was very busy these days... tonight or tomorrow I will upload the code that I use for you to see it. Yes, Python script plus bash is a powerful weapon !!

                                      1 Reply Last reply Reply Quote 1
                                      • J
                                        julenvitoria Banned @cyperghost
                                        last edited by 28 Apr 2018, 17:09

                                        @cyperghost Here are the two scripts... the first pastebin is a script in python and is originally created by George Ilyes (https://github.com/gilyes/pi-shutdown/blob/master/pishutdown.py) and is modified by PlataKoway and me to make it even more complete. Through this script I call version 0.07 of your script (the second one) in order to kill the process of the emulator that is running and then I close emulationstation in a safe way.

                                        https://pastebin.com/4FgsKK06
                                        https://pastebin.com/LpfaZh9J

                                        1 Reply Last reply Reply Quote 0
                                        • C
                                          cyperghost
                                          last edited by cyperghost 28 Apr 2018, 17:57

                                          @julenvitoria There are some things that can be improved ;)

                                          1. Primary: If you use python then make use of interrupt then you can avoid the while(true)-loop.
                                          2. Secondary: I would rather do a ES-restart than a system reboot
                                          3. Secondary: I would rather use the poweroff button for an poweroff at short press and would use the 3 second timer to do a restart/reboot ;)
                                          4. Primary: The killall emulationstation will work in most cases but you will fail if you have scraped large amount of data. Because if after 5 seconds a shutdown is initiated the file system will be set to read only and you will loose data.
                                          5. Secondary: You use lots of sudo commands. You call the k.script with sudo for ex. this isn't necassary as ES is called by user pi, morover if you call the script via rc.local you are user root (so even the sudo shutdown can be shortend to shutdown only) or if you call it via ES autostart you are user pi, and then sudo shutdown is needed ;)

                                          All in all if you are happy with your setup and it works it is okay ;)
                                          Imho take a look to Point 1, this is one of the improvements python can handle at ease ;) Because with this setup it really doesn't matter if you use python or bash ...

                                          Take a look at this example here - you will see, there is no while-loop, because you can use interrupts. Your script uses polling :) Please don't take this as an offence.

                                          J 1 Reply Last reply 28 Apr 2018, 18:11 Reply Quote 0
                                          26 out of 272
                                          • First post
                                            26/272
                                            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