[Tutorial] Get text output from bash files into your python script!
-
@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 -
@paul4tune my fault I completely forgot to switch the safe switch to on inside the nespi all working
-
@cyperghost , maybe I'm missing something really simple here, but I'm a bit confused.
I'm going to receive my NesPi+ Case, probably today, and I'm looking for the most efficient shut down script to shut the system down. I would like to shut it down completely, including the fan, while saving ES settings and metadata, and if possible saved games even if the system is shut down while using retroarch (it doesn't if you don't exit it properly, even if you saved in game) and preventing SD card corruption. As a bonus I would like to show a splash screen while shutting down, just for the looks, but that's not mandatory.
Would this script be better for that or the Multi Switch Shutdown Script as explained here? https://retropie.org.uk/forum/topic/17415/multi-switch-shutdown-script What are the fundamental differences between the two of them? Sorry if I've missed some simple post explaining just that, real life has had me busy lately and I haven't been able to find lots of time for the forums.
Thanks in advance for the answers.
-
I would like to shut it down completely, including the fan, while saving ES settings and metadata, and if possible saved games even if the system is shut down while using retroarch (it doesn't if you don't exit it properly, even if you saved in game) and preventing SD card corruption.
That's all the script is doing for you.
Would this script be better for that or the Multi Switch Shutdown Script as explained here?
The "Multi Switch Shutdown Script" (MS3) fullfills two purposes. It can act as real shutdown script then you need to call it via autostart and set parameters to it. Or it can be used as command line tool to get info about status of emulators and EmulationStation. Annother shutdown script (like the modified NESPi+ python script) can read that status and set different actions.
What are the fundamental differences between the two of them?
See I wrote that script to make setup of some power devices as easy as possible. But someone wants a LED to lid, annother wants to play a song during shutdown and you want to display a picture. So it's easier to write your own scripts and invoke MS3 to do the prober shutdown action.
The big difference between the MS3-bash script and this python is the routine how the trigger status is asked. The bash script uses the classic polling (one request in every 1 second) the python script are using interrupts which are better for mashine handling. The python script is a bit more relieable.... That's all.
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.