Forum › Forums › General › Software › How do I implement an Automated Emergency Suspend or Shutdown?
- This topic has 29 replies, 5 voices, and was last updated Feb 1-9:34 am by Lead Farmer.
-
AuthorPosts
-
September 1, 2022 at 8:07 am #87881Moderator
BobC
::sybok, You bring up things… Some that I don’t use or understand. That is a good thing because it might avoid design errors that cause problems later.
1. I tried doing it with one running script, but when run from root cron (needed to perform emergency shutdown), no YAD warnings get displayed, and only spoken warnings are played, not sound files. I settled on a user cron warning script, and root cron emergency shutdown script, both based on the same code. That isn’t as efficient, but easily allows for critical errors to be global, and users to set their own warning levels.
2. Normally I would say the defaults should originate from /etc/skel and copy to each user, but when to perform an emergency shutdown is a system wide affecting task, therefore maybe /etc/defaults might be better. I wonder where other power utilities store their settings? I am putting the logs in /tmp, but I hadn’t worried about if othewrs could access it.
3. SSH, sorry hadn’t considered it, because I don’t know much about it other than that its a communication method that allows remote access.
nd1. antiX is multiuser, therefore the warning must either go to all users or at least the user running the warning script
4. VNC is another unknown for me.I’ll post the current code. I had incorporated many of your previous suggestions. I do appreciate the suggestions as I’m learning as I go. The code is working at this point, but hard coded.
PS: I haven’t got it loading at boot, automatically yet
Attachments:
September 1, 2022 at 10:15 am #87885Member
iznit
::I wonder where other power utilities store their settings
review the manpage for dpkg-query command
One at a time, pick one of those “power utilities” to examine.
# find which package installed a selected utility dpkg-query --search /path/to/whatever_powerutility # list all files installed by that package # and scan the list looking for its settings (conf) file dpkg-query --listfiles <package-name>September 1, 2022 at 10:31 am #87886Member
iznit
::no YAD warnings get displayed
To run a GUI program from cron, we must explicitly instruct cron which display the program should use.
*/5 * * * * export DISPLAY=:0 && bash /pathto/your_script.sh
didn’t test, fingers crossed this is the missing detail
- This reply was modified 8 months, 1 week ago by iznit.
September 1, 2022 at 1:58 pm #87896ModeratorBobC
::Isnit, Yes, I had used that syntax, and the YAD text, voice and sound are all now working for both user and root.
$ crontab -l */5 * * * * env DISPLAY=:0.0 /home/bobc/bin/save_geany_files * */6 * * * env DISPLAY=:0.0 /home/bobc/bin/yad-update-check.sh */3 * * * * env DISPLAY=:0.0 /bin/bash /usr/local/bin/lowbattery-warn bobc@XPS15-I7:~ $ sudo crontab -l [sudo] password for bobc: */3 * * * * env DISPLAY=:0.0 /bin/bash /usr/local/bin/lowbattery-checkI also found a nicer voice to use with espeak. I don’t have it configured in my programs though.
sudo apt-get install mbrola mbrola-en1 espeak -v mb-en1 "Hello world"https://askubuntu.com/questions/554747/how-to-install-more-voices-to-espeak
- This reply was modified 8 months, 1 week ago by BobC.
- This reply was modified 8 months, 1 week ago by BobC.
September 1, 2022 at 3:48 pm #87899Member
sybok
September 1, 2022 at 4:00 pm #87900ModeratorBobC
::Yes, see attached. Thanks for everyone’s help.
I guess I’ll need to make the text to speech part configurable as well to allow the user to select a different voice and/or settings.
Marcelo and I tested it last night speaking the messages in Portuguese 🙂
- This reply was modified 8 months, 1 week ago by BobC.
Attachments:
September 2, 2022 at 3:30 am #87929ModeratorBobC
::I tried again with just lowbattery-check running from cron as root, and the YAD boxes don’t come up.
Very odd.
When lowbattery-warn is also there running from cron as user, the YAD boxes come up.
September 2, 2022 at 3:46 pm #87942Member
iznit
::with just lowbattery-check running from cron as root, and the YAD boxes don’t come up.
Already mentioned, cannot ass-u-me the yad command is visible via [[[ root user’s and/or cron enviroment ]]] $PATH
announce "${display_str}" /full/path/to/yad --no-pirates borders=22 ..... sleep 60Similarly, this could return a nonzero result due to espeak not being visible via the contextual $PATH
if command -v espeakSeptember 2, 2022 at 4:47 pm #87944ModeratorBobC
September 3, 2022 at 7:23 am #87963Member
sybok
::Hi, I read the script (version ‘lowbattery-cron-20220901-1.zip’) and they seem to be mostly fine.
I have six comments/suggestions/questions:A) I do again suggest to use external configuration files; this should help to avoid over-writing configuration hard-coded in the script.
The code was already present in function ‘load_ini_file’ in the post
https://www.antixforum.com/forums/topic/how-do-i-implement-an-automated-emergency-suspend-or-shutdown/#post-87877B) The ‘shellcheck’ linter/utility has some suggestions. E.g. the logic in ‘lowbattery-check’ line 80 is to be rephrased to a better defined one (at least according to the utility.
Also, it suggests to double-quote some variables which is often a safer practice.
I do have a lot of experience with spaces in filenames or paths to do it by default.
You are strongly encouraged to use ‘shellcheck’ to check your code.C) Question on full-paths e.g. /full/path/to/yad:
Will they be affected by the “merge” of some system folders to be implemented in Debian?
See e.g.
https://www.antixforum.com/forums/topic/debian-bits-from-the-release-team-on-merged-usr-and-the-bookworm-release/D) Mixing spaces and tabulators to indent the code makes it harder to read IMHO (I read it in ‘vim’ and the code is displayed as misaligned).
E) I am not sure why constructions such as the below one are used:
lowbattery-<check|warn>, line 70 and 82:display_str=$"BATTERY CRITICALLY LOW, " display_str+=$BATTERY_REM display_str+=$" MINUTES REMAINING, YOU HAVE 1 MINUTE TO PLUG IN"instead of simple
display_str=”BATTERY CRITICALLY LOW, ${BATTERY_REM} MINUTES REMAINING, YOU HAVE 1 MINUTE TO PLUG IN”
This seems to be a result of switching from re-use of some “global” value of display_str (to which one appends to) to a single use.Is this a translation (to other languages) friendly way?
F) Please add some comments to the code, e.g. I would not understand ‘lowbattery-cron’ without having followed the current thread.
That’s not good if someone would try to debug it or take over.
Adding a line explaining why mktemp is invoked and ‘env’ echoed would be very beneficial.September 3, 2022 at 1:28 pm #87978ModeratorBobC
::A) I do again suggest to use external configuration files; this should help to avoid over-writing configuration hard-coded in the script.
The code was already present in function ‘load_ini_file’ in the post
https://www.antixforum.com/forums/topic/how-do-i-implement-an-automated-emergency-suspend-or-shutdown/#post-87877Yes, definitely. I’m just trying to get the logic/design/basics working first. Not totally stable there. yet. Once that’s done, then plan a YAD script to maintain stored file with settings.
B) The ‘shellcheck’ linter/utility has some suggestions. E.g. the logic in ‘lowbattery-check’ line 80 is to be rephrased to a better defined one (at least according to the utility.
Also, it suggests to double-quote some variables which is often a safer practice.
I do have a lot of experience with spaces in filenames or paths to do it by default.
You are strongly encouraged to use ‘shellcheck’ to check your code.Yes, I’ll give it a try.
C) Question on full-paths e.g. /full/path/to/yad:
Will they be affected by the “merge” of some system folders to be implemented in Debian?
See e.g.
https://www.antixforum.com/forums/topic/debian-bits-from-the-release-team-on-merged-usr-and-the-bookworm-release/I’ll have to read it. I hope its really change that is worthwhile. systemd has been change with a big price and no reward, IMO
D) Mixing spaces and tabulators to indent the code makes it harder to read IMHO (I read it in ‘vim’ and the code is displayed as misaligned).
Yes, I hate that too. What’s worse is some people use spaces, and some with different number of spaces, others use tabs, and when you don’t have code that works to build on, you are researching and copy/pasting, and then the editor does what it wants with it. I used to use Brief under DOS for a lot of years, and had written and tweaked many macros for it. I found Grief on Linux, and put a lot of effort in, but couldn’t get it to work at all. At work, I use Notepad++ (Windows shop), and Geany seems to be the closest Linux equivalent that I’ve found. VI and EMACS are not good for me given that I spend much time on Notepad++. Is it that Geany is configured wrong or I’m doing something wrong? I’ve also tried Tilde and Jed and some others, but nothing has been great.
E) I am not sure why constructions such as the below one are used:
lowbattery-<check|warn>, line 70 and 82:display_str=$"BATTERY CRITICALLY LOW, " display_str+=$BATTERY_REM display_str+=$" MINUTES REMAINING, YOU HAVE 1 MINUTE TO PLUG IN"instead of simple
display_str=”BATTERY CRITICALLY LOW, ${BATTERY_REM} MINUTES REMAINING, YOU HAVE 1 MINUTE TO PLUG IN”
This seems to be a result of switching from re-use of some “global” value of display_str (to which one appends to) to a single use.Is this a translation (to other languages) friendly way?
Yes, so that the person translating works on each phrase, otherwise they don’t know what to do with the variable name in the middle.
F) Please add some comments to the code, e.g. I would not understand ‘lowbattery-cron’ without having followed the current thread.
That’s not good if someone would try to debug it or take over.
Adding a line explaining why mktemp is invoked and ‘env’ echoed would be very beneficial.[/quote]I was trying to find a good way to handle temporary files, because I don’t know where to put them or how to name them, etc. It was a snippet I had copied to do that.
I don’t know on env and the display variable in the program. Once the program works I’ll try taking it out and see if it still works. I forget where I found it.
Again, I appreciate the help. Trying to learn and accomplish useful things is not easy. But my laptop battery is dying, so its the perfect time to work on a battery monitor that will provide an audible alert and shutdown gracefully if necessary.
September 3, 2022 at 3:22 pm #87994Member
iznit
::sybok, today I added some reading material links to topic/debian-bits-from-the-release-team-on-merged-usr-and-the-bookworm-release/
After reading those, please tell if you do forsee an especial “merge” problem here.
September 3, 2022 at 10:06 pm #88021ModeratorBobC
::sybok,
A. On the ini file, I looked at the code and admit that I don’t understand how it works from looking at it, and will need to experiment to figure it out.
B. I did find and try the shellcheck. I will need to investigate the suggestions.
C. I do understand paths and links, but the Debian discussion and its implications are way beyond my knowledge level. I would need to see an example of how things should be done to accommodate.
September 4, 2022 at 7:41 am #88030Member
sybok
::@BobC:
A) The idea was to have an ini file name(, not necessarily with path) hard-coded in the script
cfg_log_file = '/home/bob-the-builder/.desktop-session/bat.log' # Local file
and the below grep
log_file=”$(grep ‘^[ ]*cfg_log_file[ ]*=[ ]*’ “${ini_file}” | awk -F= ‘ { print $2 } ‘ | awk -F# ‘ { print $1 } ‘ | xargs)”
gets the part enclosed by'only.
– ‘[ ]*’ handles if someone forgets about space or adds few more.
– 1st awk = get the value after the delimiter ‘=’
– 2nd awk = remove any content (comment) after the 1st delimiter ‘#’B) Great. I do have good experience (apart from double-quoting non-array variable over which one iterates), it helps me to spot some errors in the code thus making it better (similarly as Python’s pylint does).
C) The link https://wiki.debian.org/Teams/Dpkg/MergedUsr states the following:
The main goal of the merged-/usr proposal is to merge the contents of several top root directories (/bin, /sbin, /lib*) into their counterparts in /usr.
If the package/scripts/configuration do not either install into or call commands from any of those to be merged, then I guess it should be fine.
I quickly checked the code and the only thing is ‘/bin/bash’ which will hopefully be handled via backward compatibility of the merge.February 1, 2023 at 9:34 am #98656MemberLead Farmer
::@BobC I needed a scrip slimier to the one you post in post #87835, but it didn’t worked the problem is with the line
BATTINFO=<code>acpi -b</code>.I have minimal understanding in C and nothing in bash scripting, I did manged to edit your script that will work and suit my demands.
1) Battery is charging, will log the date and the battery status
2) Battery is discharging above “TOO_LOW=40”, will log the date and the battery status
3) Battery is discharging below “TOO_LOW=40”, will play a sound and show a massage to charge the computer and what the battery level is, and will log the date and the battery status
4) Battery is discharging below “CRITICALLY_LOW=20”, will do #3 but also play a sound and show a message “Actiaving suspend mode in 2 minutes if computer is not charging” if the computer will not get charged in 2 minutes the computer will suspend to save battery#!/bin/bash # lowbattery-check - low battery check and alarm # runs from root crontab # requires espeak and libreoffice for audible alerts TOO_LOW=40 CRITICALLY_LOW=20 export DISPLAY=:0.0 log_file='/home/oren/Documents/lowbattery-check.log' BATTERY_LEVEL=$(acpi -b | grep -P -o '[0-9]+(?=%)') STATUS=$(acpi -b) if [ "$(acpi -b | grep -P -o 'Discharging')" != "Discharging" ]; then echo "$(date) $STATUS" >> "${log_file}" exit 0 fi if [[ $BATTERY_LEVEL -lt $TOO_LOW ]]; then echo "$(date) $STATUS" >> "${log_file}" play /usr/lib/libreoffice/share/gallery/sounds/untie.wav yad --no-buttons --center --borders=22 --timeout=10 --title="Battery low" --text="Charge the computer, Battery is at $(acpi -b | grep -P -o '[0-9]+(?=%)')%" if [[ $BATTERY_LEVEL -lt $CRITICALLY_LOW ]]; then play /usr/lib/libreoffice/share/gallery/sounds/ups.wav yad --no-buttons --center --borders=22 --timeout=120 --title="Battery is too low" --text="Actiaving suspend mode in 2 minutes if computer is not charging" if [ "$(acpi -b | grep -P -o 'Discharging')" == "Discharging" ]; then $(sudo pm-suspend) fi fi else echo "$(date) $STATUS" >> "${log_file}" fi exit 0crontab commands will activate the scrip every 10 minutes and will delete the log file every week
*/10 * * * * /home/oren/Documents/low-battery-shutdown.sh 01 10 * * * 6 rm /home/oren/Documents/lowbattery-check.logI’m posting this if you or others will wants to use it
Edit: I change the scrip that it will only show one massage when the battery is discharging below “CRITICALLY_LOW=20”:
#!/bin/bash # lowbattery-check - low battery check and alarm # runs from root crontab # requires espeak and libreoffice for audible alerts TOO_LOW=40 CRITICALLY_LOW=20 export DISPLAY=:0.0 log_file='/home/oren/Documents/lowbattery-check.log' BATTERY_LEVEL=$(acpi -b | grep -P -o '[0-9]+(?=%)') STATUS=$(acpi -b) echo "$(date) $STATUS" >> "${log_file}" if [ "$(acpi -b | grep -P -o 'Discharging')" != "Discharging" ]; then exit 0 fi if [[ $BATTERY_LEVEL -lt $TOO_LOW && $BATTERY_LEVEL -ge $CRITICALLY_LOW ]]; then play /usr/lib/libreoffice/share/gallery/sounds/untie.wav yad --no-buttons --center --borders=22 --timeout=10 --title="Battery low" --text="Charge the computer, Battery is at $(acpi -b | grep -P -o '[0-9]+(?=%)')%" fi if [[ $BATTERY_LEVEL -lt $CRITICALLY_LOW ]]; then play /usr/lib/libreoffice/share/gallery/sounds/ups.wav yad --no-buttons --center --borders=22 --timeout=120 --title="Battery is too low" --text="Actiaving suspend mode in 2 minutes if computer is not charging" if [ "$(acpi -b | grep -P -o 'Discharging')" == "Discharging" ]; then echo "Battery is too low activating suspend mode" >> "${log_file}" $(sudo pm-suspend) fi fi exit 0- This reply was modified 3 months, 1 week ago by Lead Farmer.
- This reply was modified 3 months, 1 week ago by Lead Farmer.
- This reply was modified 3 months, 1 week ago by Lead Farmer.
-
AuthorPosts
- You must be logged in to reply to this topic.