shell scripting topic
-
@meleu Upss..
Are we not in lesson #2? -
@cyperghost Me after reading your tutorial:
-
@meleu This was my reaction
-
@meleu The interesting part is this
[[ "$(pgrep -P $emupid)" ]] && kill $(pgrep -P $emupid)
it worked flawless in my testing. I just called a bash within a bash and they were properly detected!Mausberry shutdown script v 1.58
#!/bin/bash #this is the GPIO pin connected to the lead on switch labeled OUT GPIOpin1=23 #this is the GPIO pin connected to the lead on switch labeled IN GPIOpin2=24 echo "$GPIOpin1" > /sys/class/gpio/export echo "in" > /sys/class/gpio/gpio$GPIOpin1/direction echo "$GPIOpin2" > /sys/class/gpio/export echo "out" > /sys/class/gpio/gpio$GPIOpin2/direction echo "1" > /sys/class/gpio/gpio$GPIOpin2/value while [ 1 = 1 ]; do power=$(cat /sys/class/gpio/gpio$GPIOpin1/value) if [ $power = 0 ]; then sleep 1 else # End Emulationstation if condition of running binary is true (v1.58) # v1.0 07/21/17 by cyperghost - Inital run # v1.1 07/22/17 - Added chown command to set right user permission for creating es-shutdown # v1.2 07/23/17 - Some small improvments, easier to maintain, removed echo, removed else branch # v1.5 07/27/17 - Great step to exit ES even if emulators is running by runcommand.sh are started # v1.55 07/29/17 - all kudos go to @meleu for his alltime genious RegEx hack! # v1.56 07/30/17 - All emulators will be detected. This is a full functional code equal to developing v1.7 # v1.58 08/02/17 - generel method: Use PPID to detect child PIDs now (SCUMMVM fix) # v1.7 is in work flow - This will be cleaner and better coded and is better to maintain # I just checked with SSH command - it saved my metadata! Maybe you need to extend sleeptimer! # greetings @pjft for his famous favorits and @meleu for his RegEx sniplets and his constant help! espid="$(pgrep -f "/opt/retropie/supplementary/.*/emulationstation([^.]|$)")" if [[ -e "/dev/shm/runcommand.info" ]]; then emupid="$(sed -n 4p /dev/shm/runcommand.info | tr -d '\\"' | tr '^$[]*.()|+?{}' '.')" emupid="$(pgrep -f "$emupid")" fi if [[ "$emupid" ]]; then [[ "$(pgrep -P $emupid)" ]] && kill $(pgrep -P $emupid) kill $emupid sleep 4 fi if [[ "$espid" ]]; then touch /tmp/es-shutdown && chown pi:pi /tmp/es-shutdown kill $espid exit fi # End Emulationstation if condition of running binary is true (v1.58) sudo poweroff fi done
-
@cyperghost OMG, this
-P
is a key for a strong solution. Will post something when the kids go to bed! ;) -
@meleu You would rather use
pkill -P
as pgrep and pkill are using same synthax
But I stay to the basics and this is the PID number :DI think it's now your turn to rearange and optimize the code a bit. You can also check your outfindings with the ES PID Detector - it now offers PIDs detected via PPID - it's a serious helper for such usecases.
-
@cyperghost
actually there's no need for what I was thinking. Your solution is really nice. :)Maybe some code polishing can be made, but it worked flawlessly on my tests with ScummVM. Nice finding, bro!
-
@meleu Thank you mate. Was a nice piece of work anyway but pathed with pitfalls.
You should write some more tutorials - could be a good guidance.
But for now ... good night -
@cyperghost said in shell scripting topic:
Was a nice piece of work anyway but pathed with pitfalls.
You gave me an idea!
-
@cyperghost
This is logically the same script, I just cleaned up the code (and bumped the version to v1.59): https://ghostbin.com/paste/adsrwAnd this one is the PID detector: https://ghostbin.com/paste/f8tmt
Cheers!
-
Mausberry Shutdown Script 1.595
by @meleu and @cyperghost
Final version for PowerBlock users is here!
Mausberry users may get development to v1.7x
#!/bin/bash # End Emulationstation if condition of running binary is true (v1.59) # v1.0 07/21/17 by cyperghost - Inital run # v1.1 07/22/17 - Added chown command to set right user permission for creating es-shutdown // cyperghost # v1.2 07/23/17 - Some small improvments, easier to maintain, removed echo, removed else branch // cyperghost # v1.5 07/27/17 - Great step to exit ES even if emulators is running by runcommand.sh are started // meleu # v1.55 07/29/17 - all kudos go to @meleu for his alltime genious RegEx hack! // meleu # v1.56 07/30/17 - All emulators will be detected. // meleu # v1.58 08/02/17 - generel method: Use PPID to detect child PIDs now (ScummVM fix) // cyperghost # v1.59 08/03/17 - nothing new, just polishing the code // meleu # v1.595 11/14/17 - Inserted newest emucall detection // meleu # I just checked with SSH command - it saved my metadata! Maybe you need to extend sleeptimer! # greetings @pjft for his famous favorits and @meleu for his RegEx sniplets and his constant help! #this is the GPIO pin connected to the lead on switch labeled OUT GPIOpin1=23 #this is the GPIO pin connected to the lead on switch labeled IN GPIOpin2=24 echo "$GPIOpin1" > /sys/class/gpio/export echo "in" > /sys/class/gpio/gpio$GPIOpin1/direction echo "$GPIOpin2" > /sys/class/gpio/export echo "out" > /sys/class/gpio/gpio$GPIOpin2/direction echo "1" > /sys/class/gpio/gpio$GPIOpin2/value while true; do power="$(cat /sys/class/gpio/gpio$GPIOpin1/value)" if [[ "$power" == 0 ]]; then sleep 1 else # emucall="$(sed -n 4p /dev/shm/runcommand.info | tr -d '\\"' | tr '^$[]*.()|+?{}' '.')" emucall="$(sed '4!d; s/\([\\"]\|[[:alnum:]_]\+=[^ ]* \)//g; s/[][(){}^$*.|+? ]/\\&/g' /dev/shm/runcommand.info)" if [[ -n "$emucall" ]]; then emupid="$(pgrep -f "$emucall" | tr '\n' ' ')" pkill -P "$(echo $emupid | tr ' ' ',')" kill "$emupid" sleep 4 fi espid="$(pgrep -f "/opt/retropie/supplementary/.*/emulationstation([^.]|$)")" if [[ -n "$espid" ]]; then touch /tmp/es-shutdown && chown pi:pi /tmp/es-shutdown kill "$espid" exit fi # End Emulationstation if condition of running binary is true sudo poweroff fi done
-
Answering your questions on this nerdy thread.
@cyperghost said in Mausberry Shutdown Script Doesn't Save Metadata:
@meleu
About using variables? You see I'm avoiding these and use just a bunch of them...
Is there any difference? Speed up? Memory usage?I know it's better for clear code reading.... but is that effcient?
It was just for clear code reading. Each variable have a meaningful name. In such a small script the impact on performance is null. But to be honest, using
emupid
didn't cause any impact on code reading too. :)About avoiding variables and using the same command two or more times, that would cause impact on performance.
Example:
[[ "$(pgrep -P $emupid)" ]] && kill $(pgrep -P $emupid)
Using a variable for
$(pgrep -P $emupid)
is preferable in this case.We (programmers) should always think where (and when) our code is supposed to be executed when deciding about "Memory Usage vs. Processing Usage". Some questions we should keep in mind when coding:
- The system has a good amount of RAM?
- The system has a good CPU power?
- The system has usually several users/processes sharing resources?
- How often will my code run?
- How long will my code remain running?
There are more questions, obviously.
By the way, those last 2 question are the ones that bother me so much about that infinite loop! We are looping to check a file content every single second just to execute a command to SHUT DOWN THE WHOLE SYSTEM! In my head the thought is: "this script is wasting 99.99999% of its CPU time slice!". Specially because there are ways to make a script do nothing (just wait) while that file doesn't change!
But... Well... I don't have that f!@#%ing mausberry switch, so there's no need for me to worry about it. Those guys who have it are happy with our current solution... Let's play some games! :D
This subject would be a good nerdy chat while drinking some Caipirinhas when you come to Rio.
-
Hi guys, sorry, wasn't here for a while.
I've read @meleu last post and I just checked "things", as I'm also not fond of the infinite loop. That said, I don't use mausberry or gpio either.Did you take a look at the gpio util from wiringpi ?
http://wiringpi.com/the-gpio-utility/I found this option in the doc, that may help (if I understood your need) :
gpio wfi <pin> rising/falling/both This causes GPIO to perform a non-busy wait on a single GPIO pin until it changes state to that indicated.
-
@meleu I have to say thank you for your detailed explanation. I'm currently on holidays ... so I'm a bit quieter in this forum for couple of days. I think during these days a few things will change to ES ...
But I take my portable Pi to the airport with a copy of Terranigma on it (I deeply hope that I will save humankind during may holiday)...We are looping to check a file content every single second just to execute a command to SHUT DOWN THE WHOLE SYSTEM! In my head the thought is: "this script is wasting 99.99999% of its CPU time slice!".
I understand you... about the inotify thing...
But without the real hardware it's a bit a pain ... but it's solvable imho
Maybe a chmod to the exported GPIO files will help.echo "$GPIOpin1" > /sys/class/gpio/export echo "in" >/sys/class/gpio/$GPIOpin1/direction chmod 660 /sys/class/gpio/$GPIOpin1/direction chmod 660 /sys/class/gpio/$GPIOpin1/value
This subject would be a good nerdy chat while drinking some Caipirinhas when you come to Rio.
Oh yes :) It's all about games ... As I told you it's more likely to expand to the eastern hemisphere but never say never and if I'm on buisness journey I'll surly come back to your offer.
@Sano
Welcome to this threadYes I told also to use gpio-control. wPi is also a solution but I think this approach is an overkill to just monitor ONE Gpio. I use wPi in my work as it got's powerfull IRQ functions but you have to load a whole C programm to just monitor ONE GPIO and I think that's not attentend to be called "effective".
Also think about you have to compile the thing and I think this approach isn't user friendly anymore. So way meleu want it to do is really nice. It comes very far to RT processing!
-
My new project is a portable device with 4,3" screen and I2S soundmodule.
It has a foldable screen build in and if you open it you activate an trigger....
As it is still useable with HDMI I have two/boot/config.txt
One force activates HDMI and BCM sound
The other deactivaes HDMI, activates SDTV and uses I2S soundSo I coded a bash script that would exchange the the boot files in depence to the sensors.
I've build in a LED that show status. If SDTV config is already active and the foldable monitor is open then it will boot
If the SDTV config is active and the monitor is closed then LED will blink for a few seconds ... at this time you have the chance to open the LED screen. If not the config for HDMI will be exchanged and a reboot is forced.....So here is my code
Bash is really a powerfull language, the functions call are super easy ;)This one is dedicated to you... because it wasn't possible without your help
I hope you find some time to read the code and give opinion to the structure. Are the intenions setted right, are there some things in general to improve. This is not for puplic usage so if I set a file I know that I need it and I would not check .....
-
@cyperghost dude, your coding style really improved! :-)
I did a quick look and have two or three comments about the coding (didn't focused on the logic). Will do soon. I'm on the phone now.
-
@cyperghost Looks like a ghost got your code! I was going to make my comments now and the code isn't there.
-
@meleu Sorry ;)
I setted wrong timer - this will be alive for 7 days now. I'm just busy :( But I hope to come back soon with a RetroAchivments link to you ;)
https://pastebin.com/LyA4dTwNNeustart > reboot
Blinkschleife > loop for blinking
Befehle > commands
Wahl > selection
Abfrage Hallsensor > request hall sensorsJust to understand the comments a bit ;)
-
@cyperghost I've created a gist for your script: https://gist.github.com/meleu/0ea0b96ccafdbd0a432cb5a59a8c1835
We can comment there! ;-)
-
@cyperghost I've just noticed that I don't receive notifications on a gist comment...
I answered some of your questions there. :-)
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.