[Tutorial] Get text output from bash files into your python script!
-
Here is a short tutorial,
I was asked, how to invoke command calls into python scripts.
Usually you useos.system()
call but this will just execute a command.Would it not be nice to call a fancy bash-script that give you output of running processes? Maybe a script like this one here?
You can call for example
./multi_switch.sh --es-pid
and it gives you PID of running ES binary (so you can check EmulationStation is active running), if not the value 0 is returned.Annother example: You want to check if there is a emulator running.... Therefore
./multi_switch --rc-pid
and it gives you PID of runcommand. So if runcommand is active it's very likely an emulator is running. If process runcommand isn't detected value 0 is returned.So how to get the value into my python script? Use
subprocess.check_output()
In this script the output of the echo command will be displayedEnjoy and now have fun with your dedicated shutdown scripts ;)
cyperghost# How to store output commands from subprocesses to python # cyperghost 04/24/18 # for https://retropie.org.uk import subprocess # We store ouput of command call in output # Arguments of subprocess.check_output # Command, Parameter, Parameter..... output = subprocess.check_output(['echo', '-e', '\n\tWe\tlove\t\tRETROPIE!']) # Show output results! print output `
-
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 retroflagwget -O - "https://raw.githubusercontent.com/RetroFlag/retroflag-picase/master/install.sh" | sudo bash
- Login with SSH
- Got to installed directory with
cd /opt/RetroFlag
- download the multi_switch script with
sudo wget https://raw.githubusercontent.com/crcerror/ES-generic-shutdown/master/multi_switch.sh
- Make script executable
sudo chmod +x multi_switch.sh
- Remove old script with
sudo rm SafeShutdown.py
- 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 ;)--- -
@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!
-
@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 usesudo 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 andshift
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 ;)
-
@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. -
@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 likeif [ "$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 ;) -
I just tested, and it looks like the script closes Pixel desktop launched from Ports, as well as Kodi.
-
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 ;) -
@cyperghost Am I reading that right? Your script is getting incorporated into the retroflag/NESPi+ official script? That's cool.
-
@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 ;)
-
@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!
-
@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
-
@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 retroflagwget -O - "https://raw.githubusercontent.com/RetroFlag/retroflag-picase/master/install.sh" | sudo bash
- Login with SSH
- Got to installed directory with
cd /opt/RetroFlag
- download the multi_switch script with
sudo wget https://raw.githubusercontent.com/crcerror/ES-generic-shutdown/master/multi_switch.sh
- Make script executable
sudo chmod +x multi_switch.sh
- Remove old script with
sudo rm SafeShutdown.py
- 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.
-
@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. -
@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. -
@fastpop72 I changed internal pathes from
./multi_switch.sh
, to/opt/RetroFlag/multi_switch.sh
that should work now ;) -
@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.
-
@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
-
@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!
-
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
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.