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

    [Tutorial] Get text output from bash files into your python script!

    Scheduled Pinned Locked Moved Ideas and Development
    cyperghostpython
    38 Posts 10 Posters 10.5k 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 @cyperghost
      last edited by cyperghost

      Automatic Install: I created an installer like retroflag did

      SET NESPI+ case safe shutdown switch to ON position

      use wget -O - "https://raw.githubusercontent.com/crcerror/retroflag-picase/master/install.sh" | sudo bash


      Manual install: In the case of the NESPi+ and the orignial script you can use this method, too

      Get the script from here - GITHUB

      To install manually take following steps:
      SET NESPI+ case safe shutdown switch to ON position
      0. Install original script from retroflag wget -O - "https://raw.githubusercontent.com/RetroFlag/retroflag-picase/master/install.sh" | sudo bash

      1. Login with SSH
      2. Got to installed directory with cd /opt/RetroFlag
      3. download the multi_switch script with sudo wget https://raw.githubusercontent.com/crcerror/ES-generic-shutdown/master/multi_switch.sh
      4. Make script executable sudo chmod +x multi_switch.sh
      5. Remove old script with sudo rm SafeShutdown.py
      6. Get new script with sudo wget https://raw.githubusercontent.com/crcerror/retroflag-picase/master/SafeShutdown.py

      Small update 03.07.2018

      1. If you press restart if emulator is currently running, then you will be kicked back to ES main menu
      2. If you press restart in ES main screen, ES will be restartet (no reboot!), good for quick saving metadata or internal saves.
      3. If you press power-off then Raspberry will shutdown
      
      All metadata is always saved
      Best -> cyperghost
      

      Python code: SafeShutdown.py

      #!/usr/bin/env python3
      from gpiozero import Button, LED
      import os 
      from signal import pause
      import subprocess
      
      powerPin = 3 
      resetPin = 2 
      ledPin = 14 
      powerenPin = 4 
      hold = 1
      led = LED(ledPin)
      led.on()
      power = LED(powerenPin)
      power.on()
      
      #functions that handle button events
      def when_pressed():
       led.blink(.2,.2)
       output = int(subprocess.check_output(['/opt/RetroFlag/multi_switch.sh', '--es-pid']))
       if output:
           os.system("/opt/RetroFlag/multi_switch.sh --es-poweroff")
       else:
           os.system("sudo shutdown -h now")
          
      def when_released():
       led.on()
      
      def reboot():
       output = int(subprocess.check_output(['/opt/RetroFlag/multi_switch.sh', '--es-pid']))
       output_rc = int(subprocess.check_output(['/opt/RetroFlag/multi_switch.sh', '--rc-pid']))
       if output_rc:
           os.system("/opt/RetroFlag/multi_switch.sh --closeemu")
       elif output:
           os.system("/opt/RetroFlag/multi_switch.sh --es-restart")
       else:
           os.system("sudo reboot")
       
      btn = Button(powerPin, hold_time=hold)
      rebootBtn = Button(resetPin)
      rebootBtn.when_pressed = reboot 
      btn.when_pressed = when_pressed
      btn.when_released = when_released
      pause()
      

      Python and the GPIOZERO libary seems to be overloaded. As I did not create this script.
      But it demonstrates how bash files output can be used to do actions ;)---

      caver01C F C 3 Replies Last reply Reply Quote 0
      • caver01C
        caver01 @cyperghost
        last edited by caver01

        @cyperghost I apologize for not commenting earlier, as I have been busy. I was finally able to start testing your mutli_switch with my setup and I have to say, I really love it.

        I am commenting here because this thread fits better with my implementation. I did my initial tests last night using my NESPi case build. You might recall, I am using the original NESPi, with a Mausberry switch added. I have not resolved to soft shutdown on this case yet. I may try the diode solution or my transistor method for that some day.

        Anyway, in the thread linked above, I mentioned my python script. This makes two os.system() calls. I have replaced these with calls to your multi_switch.sh. The first one is for the power button, so I am using the --poweroff parameter, and the second is my reset which I like to use for closing down emulators, so that one gets a --closeemu.

        Both of these functions work perfectly. I am very impressed. Previously, I was using the old way of killing the emulator PIDs and this would fail on some systems, but yours works on everything I have tried so far. I have not tested it with Kodi or the Pixel Desktop. Have you tested either of these yet?

        Although I have both a Mausberry and the NESPi case, I don't see myself using these parts of your script, as I want to use Python edge detection for GPIO interactions. But, I LOVE the idea of having a single script to handle all of the rest. Shutdown, reboot, kill ES, kill emus--it is really clean to handle each of these functions with a single script using command line parameters. Nicely done.

        I was thinking of how I might use it on my other system. My roadcase cocktail arcade also has a Mausberry with the transistor to trigger it during the soft shutdown. One thing you could add to your script is the GPIO-shutoff.

        Finally, I noticed on your github that you still want to build a config file. I was thinking about some of the GPIO specific functions and wondering if you could add more parameters for them to cover the GPIO pins choices as additional command line parameters. That way, nobody would have to edit the script and you might not even need a config file.

        Well Done!

        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 Thank you for your words.

          I apologize for not commenting earlier, as I have been busy. I was finally able to start testing your mutli_switch with my setup and I have to say, I really love it.

          No problem mate. It was also fun to create this as it is super easy to adapt switches now and get status of an unheaded system just by SSH commands.

          Anyway, in the thread linked above, I mentioned my python script. This makes two os.system() calls. I have replaced these with calls to your multi_switch.sh. The first one is for the power button, so I am using the --poweroff parameter, and the second is my reset which I like to use for closing down emulators, so that one gets a --closeemu.
          Both of these functions work perfectly. I am very impressed.

          Yes that's caused by the method to use runcommand.sh as entrypoint and to build an array of all child processes it initiates. So it's a more robust method then to interpret the log files as long as the emulator was initiated by runcommand.

          But you can do much more. With the reset button you can ask for status of --es-pid and --rc-pid. If an emulator is running then use --closeemu to get back to ES and if --es-pid is alongside without valid --rc-pid use --es-restart to restart ES only. both PIDs aren't valid then use sudo reboot to reboot system. So you use the reset-button most effective.

          I have not tested it with Kodi or the Pixel Desktop. Have you tested either of these yet?

          No none - but as long is they are called by runcommand the chances are good to catch the processes and to terminate them.

          Although I have both a Mausberry and the NESPi case, I don't see myself using these parts of your script, as I want to use Python edge detection for GPIO interactions. But, I LOVE the idea of having a single script to handle all of the rest. Shutdown, reboot, kill ES, kill emus--it is really clean to handle each of these functions with a single script using command line parameters. Nicely done.

          Yes, that was the main intention in doing this. Rather to recode all codes again and again in different languages use language that can handle the stdout of an command. A dirty workaround would be to call the script and to redirect output to a file. This will work in EVERY coding I know.

          And I don't like it to have several scripts that are doing just one function. I named the script the swiss army knife for ES shutdowns.... or just a small libary.

          I was thinking of how I might use it on my other system. My roadcase cocktail arcade also has a Mausberry with the transistor to trigger it during the soft shutdown. One thing you could add to your script is the GPIO-shutoff.

          No the GPIO-shutoff is a systemd service. This won't work for such a simple script... It must be loaded as service via systemctl command then this will surly work, too. But that's not my intention and will likely overstrain users like me. Moreover it's real horror to maintain this.... The deeper is it implented into system the more it would break something. In worst case an ever shutoff loop because the settings are wrong.

          Finally, I noticed on your github that you still want to build a config file. I was thinking about some of the GPIO specific functions and wondering if you could add more parameters for them to cover the GPIO pins choices as additional command line parameters. That way, nobody would have to edit the script and you might not even need a config file.

          Yes... I think I will add command line paramters like .... --mausberry poweronctrl=23 powerswitch=24 or something alike.
          That's very easy to do... with $# you get number of passed parameters and shift can exchange parameters.

          Take the --mausberry poweronctrl=23 powerswitch=24 example
          So --mausberry is always $1 as "base command"
          then shift changes $2 to get on first position (=$1) and $3 will be second parameter (=$2) and now you can analyse content of these parameters and extract GPIO numbering.
          So I can say: Mausberry needs at least two commands. If there are more then show error. If there is none, then use default settings.....

          More to come .... and with greetings to @meleu - hope you'll find some time for doing some neat coding ;)

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

            @cyperghost said in [Tutorial] Get text output from bash files into your python script!:

            No the GPIO-shutoff is a systemd service. This won't work for such a simple script... It must be loaded as service via systemctl command then this will surly work, too. But that's not my intention and will likely overstrain users like me. Moreover it's real horror to maintain this.... The deeper is it implented into system the more it would break something. In worst case an ever shutoff loop because the settings are wrong.

            I was not thinking of replacing meleu's service. The service idea still works fine, but inside the service, we still need to call a killes.sh so that call might as well point to the multi_switch. Maybe a separate parameter that kills ES without also shutting down (since it would already be shutting down). I just thought another parameter could run a function that triggers a GPIO pin for those that need it. As it is, I still need to keep a killes.sh around just to make the service close ES and trigger my diode/transistor.

            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 Now I got you....
              Well the I used the systmed thing two times.
              For my Mausberry to simulate keypress thorugh a diode and to my Pimoroni OnOffShims....

              If you place a script into /lib/systemd/system-shutdown/-folder, make it executable it will always be executed during reboot and poweroff (=system halt). To precise usecase make an if/then clause like

              if [ "$1" = "poweroff" ]; then
               --- now set you GPIO here ---
              fi
              

              It is the importanst thing for such scripts!.... Use the shebang #!/bin/sh in FIRST LINE otherwise the script will fail to run.

              But in this state most system functions are already shutoff ... I think Wifi, LAN and even write status to filesystem is set to read only. So with this "service" you can just do some bare operations like set GPIOs but no write commands for ES.

              but inside the service, we still need to call a killes.sh so that call might as well point to the multi_switch

              Yes that could be possible

              Maybe a separate parameter that kills ES without also shutting down (since it would already be shutting down)

              Well the multi_script won't initiate any shutdowns or so. The shutdowns are performed by /opt/retropie/supplementary/emulationstation/emulationstation.sh script ;) So I terminate ES binary and exit the multi_switch. then emulationstation.sh takes over ;)

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

                I just tested, and it looks like the script closes Pixel desktop launched from Ports, as well as Kodi.

                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

                  Hey I've my first follower here - even the vatiable names are "very" equal and it looks like "I don't know what it does but if I copy the calls 1 by 1 it works" ;)
                  That's amazing ... how people get inspired to solve these shutdown problems ;) So this guy took the approach ... +1 for him ;)

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

                    @cyperghost Am I reading that right? Your script is getting incorporated into the retroflag/NESPi+ official script? That's cool.

                    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 No... I think not. But someone ported it into python coding ;) I know that because he uses same variabels ;) Or same pgrep calls (you can use pidof emulationstation and get PID directly. But indeed amazing and a bit curious ;)

                      I made the script puplic via github and so it can be used by everyone. Maybe retroflag wants to use that, too. But I doubt that ;)

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

                        @cyperghost Ok, but that PR you linked to is an attempt to merge code into the Retroflag Master, which is the official RetroFlag repo linked on their website. If accepted, everyone will benefit from your technique!

                        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 Well yes... but from the companies view - I won't trust a script that is not written by myself. It needs to be maintained and so you (as company) may run into a way of relation. So keep it simple and other users may read the PR and modify the scipts ;) That's the best way and keeps away from trouble

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

                            @cyperghost said in [Tutorial] Get text output from bash files into your python script!:

                            In the case of the NESPi+ and the orignial script you can use this method, too

                            Get the script from here - GITHUB

                            To install take following steps:
                            0. Install original script from retroflag wget -O - "https://raw.githubusercontent.com/RetroFlag/retroflag-picase/master/install.sh" | sudo bash

                            1. Login with SSH
                            2. Got to installed directory with cd /opt/RetroFlag
                            3. download the multi_switch script with sudo wget https://raw.githubusercontent.com/crcerror/ES-generic-shutdown/master/multi_switch.sh
                            4. Make script executable sudo chmod +x multi_switch.sh
                            5. Remove old script with sudo rm SafeShutdown.py
                            6. Get new script with sudo wget https://raw.githubusercontent.com/crcerror/retroflag-picase/master/SafeShutdown.py

                            Python code: SafeShutdown.py

                            #!/usr/bin/env python3
                            from gpiozero import Button, LED
                            import os 
                            from signal import pause
                            import subprocess
                            
                            powerPin = 3 
                            resetPin = 2 
                            ledPin = 14 
                            powerenPin = 4 
                            hold = 1
                            led = LED(ledPin)
                            led.on()
                            power = LED(powerenPin)
                            power.on()
                            
                            #functions that handle button events
                            def when_pressed():
                             led.blink(.2,.2)
                             output = int(subprocess.check_output(['./multi_switch.sh', '--es-pid']))
                             if output:
                                 os.system("./multi_switch.sh --es-poweroff")
                             else:
                                 os.system("sudo shutdown -h now")
                                
                            def when_released():
                             led.on()
                            
                            def reboot():
                             output = int(subprocess.check_output(['./multi_switch.sh', '--es-pid']))
                             if output:
                                 os.system("./multi_switch.sh --es-restart")
                             else:
                                 os.system("sudo reboot")
                             
                            btn = Button(powerPin, hold_time=hold)
                            rebootBtn = Button(resetPin)
                            rebootBtn.when_pressed = reboot 
                            btn.when_pressed = when_pressed
                            btn.when_released = when_released
                            pause()
                            

                            Python and the GPIOZERO libary seems to be overloaded. As I did not create this script.
                            But it demonstrates how bash files output can be used to do actions ;)---

                            Just tried this procedure with my new Nespi Case+, then rebooted. Unfortunately now when i turn off the switch it blinks the red led of the case and does nothing. If i press the reset button does nothing. I had to revert to original Nespi script.

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

                              @fastpop72 Is the multi_switch.sh script located into same folder as the python script? Is it made executable? It works for me but I don't give support for that.

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

                                @cyperghost said in [Tutorial] Get text output from bash files into your python script!:

                                @fastpop72 Is the multi_switch.sh script located into same folder as the python script? Is it made executable? It works for me but I don't give support for that.

                                Sure it was in the same folder "/opt/RetroFlag", i deleted the original SafeShutdown python script, installed yours, changed exec permission with chmod, but when i press the switch to turn off the led start blinking but does nothing. Reset neither.
                                I noticed this discussion started 2 months ago... maybe the original nespi python script has changed since and your mod doesn't work anymore... no problem, i reverted to original nespy script.

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

                                  @fastpop72 I changed internal pathes from ./multi_switch.sh, to /opt/RetroFlag/multi_switch.sh that should work now ;)

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

                                    @cyperghost said in [Tutorial] Get text output from bash files into your python script!:

                                    @fastpop72 I changed internal pathes from ./multi_switch.sh, to /opt/RetroFlag/multi_switch.sh

                                    Wow... maybe your modified SafeShutdown script doesn't recall correctly the other multiswitch file if the path is not complete? I will give it a try and let you know.

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

                                      @fastpop72 I've improved the script a bit for more natural behaviour

                                      1. If you press restart if emulator is currently running, then you will be kicked back to ES main menu
                                      2. If you press restart in ES main screen, ES will be restartet (no reboot!), good for quick saving metadata or internal saves.
                                      3. If you press power-off then Raspberry will shutdown
                                      
                                      All metadata is always saved
                                      Best -> cyperghost
                                      
                                      F 1 Reply Last reply Reply Quote 1
                                      • F
                                        fastpop72 @cyperghost
                                        last edited by

                                        @cyperghost said in [Tutorial] Get text output from bash files into your python script!:

                                        @fastpop72 I've improved the script a bit for more natural behaviour

                                        1. If you press restart if emulator is currently running, then you will be kicked back to ES main menu
                                        2. If you press restart in ES main screen, ES will be restartet (no reboot!), good for quick saving metadata or internal saves.
                                        3. If you press power-off then Raspberry will shutdown
                                        
                                        All metadata is always saved
                                        Best -> cyperghost
                                        
                                        

                                        Your new modified script WORKS LIKE A CHARM NOW!!! It's exactly what i'm looking for... Now i can press my Nespi+ reset button to exit any game and return to ES! And the Power/shutdown button turn off the whole system VERY QUICKLY!! Few seconds and the Nespi+ is completely off! THANKS THANKS THANKS!

                                        1 Reply Last reply Reply Quote 1
                                        • P
                                          paul4tune
                                          last edited by

                                          I have tried the manual install, as well as your auto install, I for the life of me cant get it to work and i don't know why.
                                          I am new to the retropie world, i have a nespi plus, pi3b+, 128gig card running a retro gaming image.
                                          I'm running PuTTY for the SSH connection

                                          P 1 Reply Last reply Reply Quote 0
                                          • P
                                            paul4tune @paul4tune
                                            last edited by

                                            @paul4tune my fault I completely forgot to switch the safe switch to on inside the nespi all working

                                            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.