Multi Switch Shutdown Script!
-
@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
-
@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 forruncommand.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.
-
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
-
@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
-
@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 glowuse
raspi-gpio set GPIONrLEDCONNECTED op dh
to dim LED
useraspi-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
-
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:
- This script uses
runcommand.sh
process as entrypoint - It generates an array of child-PIDs called through
runcommand.sh
- It shifts array backwards
- It sends TERM signal to all PIDs called by runcommand.sh (from newest to oldest)
- It waits if PID is finished
- Emulator is terminated
So in practice:
runcommand.sh [PID 1234] calls emulator lr-xxxxxx [PID 1236]
So PID 1236 is terminatedsometimes 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 viaruncommand.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
- This script uses
-
@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 asmulti_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.
-
@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 !!
-
@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.
-
@julenvitoria There are some things that can be improved ;)
- Primary: If you use python then make use of interrupt then you can avoid the while(true)-loop.
- Secondary: I would rather do a ES-restart than a system reboot
- 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 ;)
- 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.
- 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.
-
@cyperghost Oh, thanks very much! I'll take a look to the link. You can be calm, I always receive in a good way a constructive criticism!! My skills of python are not very good but I am learning... :D
-
@julenvitoria I'm with you.
-
I just got a nespi+ case, and installed your script (I never installed the one from nespi). It restarts ES fine, but I don't think it's doing the safe shutdown correctly. When I press the power button, it goes straight to a black screen and shuts off. I don't see the screen output doing it's safe shutdown processes.
I used the instructions here to install: https://retropie.org.uk/forum/topic/17461/nespi-case-reset-works-power-doesnt/16
I did install raspi-gpio as well.
If I run this at the command line, I get a pid: ./multi_switch.sh --es-pid
I tried running the shutdown command from the command line as well (./multi_switch.sh --es-poweroff), but it goes straight to a black screen as well...not a safe shutdown.What could be wrong?
I have an RPi3, running RetroPie 4.3.
-
@gollumer Well... did you checked if your favourites are saved? I regard your feedback as the script is doing it's work fine in a NESPI+ case. Let me explain: I tested several times on my system (It's exactly like yours: RPi3 and RetroPie 4.3) and I was able to save favourites and even scraped data with the switches
--es-poweroff
you used, too. So I see no fails or flaws. The shutdown is incredible fast because it terminates each session as long as the PIDs are not valid anymore.
The trick is, that if the ES binary received the -TERM signal annother script (it's internal in ES) takes over and initiates the shutdown. So it behaves exactly as you restart/reboot/shutoff via ES's QUIT from Main Menu!You can view the script here
nano /opt/retropie/supplementary/emulationstation/emulationstation.sh
- this is responsible for restart ES and other system shutdowns.But please tell me if following things are working (because NESPi+ is still WIP, caused by I don't have this device here in range for testing, so everything is done with code reading and some photographs of the connectivity to the GPIO heads)
- You press POWER ON and the system starts, LED will glow if script is loaded
- You press POWER OFF inside ES (even with emulators running) and the system performs a save shutdown (with metadata, favourites .... saved) - LED blinks 4 times and stays off
- You press RESET with emulator running and you will be returned to ES
- You press RESET within ES and it restarts ES (NO REBOOT!!!)
Thanks for your support!
-
@cyperghost said in Multi Switch Shutdown Script!:
But please tell me if following things are working (because NESPi+ is still WIP, caused by I don't have this device here in range for testing, so everything is done with code reading and some photographs of the connectivity to the GPIO heads)
You press POWER ON and the system starts, LED will glow if script is loaded
You press POWER OFF inside ES (even with emulators running) and the system performs a save shutdown (with metadata, favourites .... saved) - LED blinks 4 times and stays off
You press RESET with emulator running and you will be returned to ES
You press RESET within ES and it restarts ES (NO REBOOT!!!)Thank you for your response. Here are the answers to your questions:
-
Yes, POWER ON = the system starts, and LED lights up.
-
No, POWER OFF inside ES did not perform a safe shutdown, no metadata saved. The LED blinked 4 times, then the screen went black. Prior to pressing POWER OFF, I had added a game to my Favorites collection, and verified that it was there. After turning off, then turning back on, the game was no longer in my Favorites collection.
-
No, RESET with emulator running does nothing.
-
Yes, RESET within ES (but not emulator running) restarts ES without rebooting.
So, as you can see, it's partially working, but not completely (and the most important one...#2 (safe shutdown)...is not working.
-
-
@gollumer Okay thanks.
Script works!
It is essential to set the switch in the NESPi+ CASE in ON position!
-
Update: I opened up the nespi+, turned the switch from ON to OFF, then back to ON, and now it's saving metadata! Seems that it wasn't completely in the ON position. Also, RESET from within an emulator now works as well. (I think the issue with that before was I was just pressing it quickly, not holding it for a second...)
So, problem solved.
-
@gollumer Perfect! As I said - some people like @meleu @TMNTturtlguy and me spent tons of time to elaborate a working solution! So I was pretty sure the script works!
Annother questions:
Do you have a fan installed in your system?
Do you load the script perautostart.sh
or viarc.local
? Both versions do work but I prefer always the autostart solution. -
Yes, I have a fan. I installed it on the 3.3v connector instead of 5v so that it's not as loud. I'm loading the script in autostart.sh.
-
I installed this script with my Nespi+ case and it works perfectly!
I also have a fan. If I plug it into a 3.3v pin, what scripts are available to control it?
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.