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 116.2k 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.
    • cyperghostC
      cyperghost @cloudlink
      last edited by cyperghost

      @cloudlink Well I tested several times now. And I've achived always flawless shutdown and poweroff with no loss of metadata ... So safe shutdown works!

      You can see if you restart ES ... as the file system is initiated you will see "retropie: clean" or "dirt bit set!"

      If the dirt bit is set! Then there were issues with the shutdown.

      In my setup it never accours. I would assume you've downloaded the newest version but did not remove the older one. So the new one is named multi_switch.sh.1

      Enter:
      cd /home/pi/RetroPie/scripts
      then rm multi_switch.*
      This will remove all copies of multi_switch.

      Then follow this guide here
      I've written it for NESPi+. If you made all steps right then you just need Pt. 3 of the guide!

      Btw: My NESPi+ board version is USB2 V2, 18/01/18

      caver01C 1 Reply Last reply Reply Quote 0
      • caver01C
        caver01 @cyperghost
        last edited by

        @cyperghost So, does this mean that with the proper software configuration using your latest updates etc., the NESPi+ case can function with full shutdown without the need for a Mauseberry or similar hardware hack/addition?

        My 4-player cocktail style cabinet built as a custom "roadcase"

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

          @caver01 As far is a tested: Yes, it makes a full power cut.

          There aren't any LED activites anymore (red LED for power on Raspberry for ex.) and 5V rail is also shut off. So this seems to work. It seems to work very similar to the Pimoroni ShimOnOff ... You need also a GPIO set to 0 that immediatly cuts power also via systemd, too ;)

          caver01C 1 Reply Last reply Reply Quote 0
          • caver01C
            caver01 @cyperghost
            last edited by

            @cyperghost Amazing. This little case just keeps getting better and better. The updated script and NESPi+ means you can save $20 by not needing a power button circuit, and you save all of the modification time/risk, and you save the space inside the case for all of the rewiring etc.

            About the only thing worth doing to these now is moving the ethernet port to the back.

            My 4-player cocktail style cabinet built as a custom "roadcase"

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

              @caver01 If you want to save money definitly a good choice! A user from github told me, that in python script from retroflag it is possible to cut power, without the systemd-service. Because he said, my script works with reboot/save shutdown and so on... but the fan is still blowing.

              Therefore I used the systemd-service to cut power and now the fan is shutdown by poweroff - same was reported by @gollumer. He made constant report that helped a lot. On my first tests I had exact the same issues as he reported - The Pie was shutdown but only in idle-mode (means 5V rail on, red LED on Pie also on). But with the introduction of the systemd-script the fan-off issue is also solved and we obtain a complete unpowered device (means 5V rail off, red LED on Pie also off)

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

                @caver01 I'm currently working on a version that supports command line options.

                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: multi_switch.sh --mausberry this will use GPIO23 for power button and GPIO24 for power ON control.
                2. multi_switch.sh --mausberry powerbtn=17 this will use GPIO17 for power button and GPIO24 for power ON control still as default
                3. multi_switch.sh --mausberry powerctrl=4 powerbtn=3 this will use GPIO3 for power button and GPIO4 for power ON control
                4. multi_switch.sh --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.

                Also better support pages will be contained. So you get support for each command or device if you just enter ./multi_switch.sh --nespi+ help for example. I will integrate this into a later version so we can rush to v1.00.

                Annother thing I may add: --systemd value to create compatibility with Shutdown Service by @meleu. So you enter multi_switch.sh --systemdinto the service file:
                instead : ExecStop=/etc/killes.sh
                you enter: ExecStop=/path/to/multi_switch.sh --systemd
                Then you are completly independent of the power switch settings from here and can use python or any other coding language to to get ES gracefully shutdown just by using sudo poweroff or sudo shutdown -h now. The script is in no way abolished by revitalising the shutdown service because you can still reset/reboot ES with commands this script provides ;)

                And then maybe some installer scripts ;)
                Much better documentatation ;)
                So feedback is appreaciated ;)

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

                  Version 0.85 is now available.....

                  This is a huge step!

                  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

                  1 Reply Last reply Reply Quote 0
                  • C
                    cloudlink
                    last edited by

                    Awesome!
                    With Version 0.85, are the installation instructions still the same?

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

                      @cloudlink Yes! Remove old version before and just download the newest. So first use point 3.1 and then do point 3.0 from instructions ;) But there are just a small things improved. It's just better documented now and some calls are much much cleaner. The biggest point is the possibility to setup GPIOs via commandline now and I differ between kill -15 and kill -9 signal now. I try to use kill -15 in first instance. If after 2 seconds the process is still active... kill level 9 is used. kill -9 can make some problems and it should only be used as last resort.
                      So I use this only for emulators.... you can test for example with pifba. This isn't closed with kill -15 so after 2 seconds the emulator will close caused by kill -9 signal.

                      @caver01 I tried to use a kind of systemd-like call. But I wasn't able to integrate this into the shutdown service. The call to should be ExecStop=path/to/multi_switch.sh --es-systemd but It didn't work. Even a call to killes.sh that calls the multi_switch.sh and waits several seconds.... Did not work! I hope the service can be done to rework again!

                      caver01C 1 Reply Last reply Reply Quote 0
                      • caver01C
                        caver01 @cyperghost
                        last edited by

                        @cyperghost said in Multi Switch Shutdown Script!:

                        @caver01 I tried to use a kind of systemd-like call. But I wasn't able to integrate this into the shutdown service. The call to should be ExecStop=path/to/multi_switch.sh --es-systemd but It didn't work. Even a call to killes.sh that calls the multi_switch.sh and waits several seconds.... Did not work!

                        Bummer. This was exactly what I wanted to do. I wonder if needed subsystems are closing before the call.

                        My 4-player cocktail style cabinet built as a custom "roadcase"

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

                          @caver01 Maybe you can test and report? For me this did not work and I may hide it (it won't be displayed as option in help pages) if the --es-systemd switch will not run. It should in theoretically work the same way as @meleu's service (maybe you can add the sleep timer) but I had no success.

                          caver01C 1 Reply Last reply Reply Quote 0
                          • caver01C
                            caver01 @cyperghost
                            last edited by

                            @cyperghost I see what you were doing. . . you were actually trying to define the service using multi_switch.sh --es-systemd. I guess I was simply going to call the multi_switch.shfrom meleu's service instead. I need to look at it and see how it is setup now. I wonder if the problem is what others are seeing--that even meleu's service is not working correctly now.

                            My 4-player cocktail style cabinet built as a custom "roadcase"

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

                              @caver01 Yes...

                              My first try was the ExecStop=

                              then later I used the killes.sh script and wrote just

                              #!/bin/bash
                              /path/to/multi_switch.sh --es-systemd
                              sleep 10
                              

                              Made it executable with chmod a+x .... Did not work. So I think the service isn't working anymore. Maybe I've done something wrong?
                              I think there is no difference to call a bash file with parameters or to call it without ;) But I'm not sure if these systemd services can parse commandlines.... I think yes! But that's the difference of a hobby user like me and someone who really digs deep into unix systems ;)

                              caver01C 1 Reply Last reply Reply Quote 0
                              • caver01C
                                caver01 @cyperghost
                                last edited by

                                @cyperghost Ok, so on my main arcade build (custom case, Mausberry Switch, dedicated momentary power button, GPIO-->transistor trigger), I dropped in your multi_switch. I updating my Python script that watches the GPIO triggered by the switch on the Mausberry. Now, instead of doing the traditional kill PIDs and killES, I simply call multi_switch --es-poweroff. Fast and done. The Pi shuts down, Mausberry detects the drop on the other GPIO and cuts the power.

                                For soft shutdown, I updated Meleu's service. My version of the service has been doing two things: First, it triggers the Mausberry via GPIO to the transistor which in turn closes the power switch. Second, it did the old emulator PID lookup and kill, plus killES. These commands got replaced with the multi_switch --es-poweroff call again.

                                This works great, although I should say that my system is still running the previous RetroPie image (4.3, Jessie).

                                We know that using the switch works great. We also know that soft shutdown also leads to the Mausberry cutting power correctly. So, I am certain that the service is triggering the transistor. What I don't know is if the trigger is getting picked up by the python script FIRST which calls multi_switch or if the call is being made by the service. Either way, my metadata is getting saved.

                                I could try removing the call to multi_switch from meleu's service. That way, the only thing the service is doing is triggering the switch. I may just leave it alone.

                                My 4-player cocktail style cabinet built as a custom "roadcase"

                                cyperghostC 2 Replies Last reply Reply Quote 0
                                • cyperghostC
                                  cyperghost @caver01
                                  last edited by cyperghost

                                  @caver01 Yes the scripts are called by the service but somehow the sleep commands are ignored.

                                  You can place the transistor call to /lib/systemd/system-shutdown/ as script.
                                  Then make the script executable.... Be aware to place the shebang #!/bin/sh in first line!

                                  You can use the same technique like I've done in Exposed Shim ON OFF

                                  With the script you can differ between reboots, shutdowns.... so the trigger can be called only by shutdown (=poweroff) and reboots.

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

                                    @caver01 said in Multi Switch Shutdown Script!:

                                    We know that using the switch works great. We also know that soft shutdown also leads to the Mausberry cutting power correctly. So, I am certain that the service is triggering the transistor. What I don't know is if the trigger is getting picked up by the python script FIRST which calls multi_switch or if the call is being made by the service. Either way, my metadata is getting saved.

                                    So with this setup following happens.
                                    The script is first triggered by your python script. It calls in first instance the multi_switch. The multi_switch closes all emulators and wait for ES to finish (so all metadata is saved!) then multi_switch is quit and gives control back to ES-script. The ES-script checks for apperance of files in /tmp-folder, this files trigger the further actions. It can be a restart of ES, a reboot of the system or a system shutdown!

                                    After that the service of meleu is initiated. So the service takes action AFTER all metadata is saved. I wasn't able to run the service in a proper way... so I can't say if it works proper in general :(

                                    caver01C 1 Reply Last reply Reply Quote 0
                                    • caver01C
                                      caver01 @cyperghost
                                      last edited by

                                      @cyperghost said in Multi Switch Shutdown Script!:

                                      The script is first triggered by your python script

                                      Not during a soft shutdown. For software-initiated, my python is not seeing anything until it detects the button "pressed" by the transistor. So, for soft shutdown, Meleu's service is triggering that. It is then a race between python calling multi_switch and the service calling multi_switch.

                                      My 4-player cocktail style cabinet built as a custom "roadcase"

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

                                        @caver01 Oh yes ;) Even on a soft shutdown it is clear what happens.
                                        ES writes a metadta and file into /TMP folder and the binary quits. After that, shutdown is initiated and then meleus service takes action. Then if multi_switch is called indeed a seconds shutdown is initiated ...

                                        But I don't understand what you mean with "race" of the scripts. If you use the --es-systemdparameter I don't initiate annother shutdown.

                                        caver01C 1 Reply Last reply Reply Quote 0
                                        • caver01C
                                          caver01 @cyperghost
                                          last edited by

                                          @cyperghost It is a race because before the script in Meleu's service calls multi_switch, it first triggers my GPIO transistor. When that happens, my python script calls multi_switch. So, one or the other call is going to make sure everything closes. I don't know which one finishes first.

                                          My 4-player cocktail style cabinet built as a custom "roadcase"

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

                                            @caver01 Ahhh........!
                                            Yes!

                                            That makes it clear. But the solution for this is very easy. If you place a script to /lib/systemd/system-shutdown you call from there the trigger to your transisitor then the filesystem is already set to read only. Read more about here: Linux Stack Exchange and in this (late) state the system is miliseconds away for going to powerdown. That is the right moment to send signal to GPIO to "push" the button. The service also pushes arguments to callers. So the script can decide between shutdowns, reboots and so you call the trigger just in poweroff state.

                                            I did it here.... Exposed Shim ON OFF and also here in my Yet annother NESPi case mod also with a Mausberry build in and so there is no race against two shutdowns ;)

                                            So in your case remove the "GPIO trigger"-part from meleus service right to annother script that is placed to /lib/systemd/system-shutdown/-folder. You need sudo command for this as this files are property of root.

                                            caver01C 1 Reply Last reply 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.