Page 1 of 1
I would like a warning before shutting down the computer
Posted: Fri Jun 02, 2023 8:33 am
by Wallon
Dear users,
It's silly, there have been several times when I've closed the computer without realising that I'd left LibreOffice documents open.
When I restart, I can't always recover the documents. Naturally, in the LibreOffice settings, I've set up a backup of open files and a copy every 10 minutes. It's my fault, I should be more careful, I know.
Isn't there a way of receiving a notification saying that programmes are open before shutting down the computer?
Best regards,
Wallon
Re: I would like a warning before shutting down the computer
Posted: Fri Jun 02, 2023 9:13 am
by j2mcgreg
@Wallon
In the XFCE version, there's already a warning in place. You just have to get into the habit of checking the panel for open applications before you initiate a shutdown. In the attachment, the little horizontal bars above the Chrome and Writer icons indicate that they are active and need to be closed prior to shutdown.
Screenshot_2023-06-02_09-00-14.png
Re: I would like a warning before shutting down the computer
Posted: Fri Jun 02, 2023 11:05 am
by CharlesV
Wallon wrote: Fri Jun 02, 2023 8:33 am
Dear users,
It's silly, there have been several times when I've closed the computer without realising that I'd left LibreOffice documents open.
When I restart, I can't always recover the documents. Naturally, in the LibreOffice settings, I've set up a backup of open files and a copy every 10 minutes. It's my fault, I should be more careful, I know.
Isn't there a way of receiving a notification saying that programmes are open before shutting down the computer?
Best regards,
Wallon
Since the shutdown is a script, (and there is a way to run a script in-front of that even!), I am sure there is a way to look for specific open applications, throw a notification and then prevent the shutdown.
This is an interesting thought, and one I want to investigate.
Re: I would like a warning before shutting down the computer
Posted: Fri Jun 02, 2023 11:40 am
by MXRobo
Re:
when I've closed the computer….
Do you mean:
A - Power Manager>System>Laptop Lid
does it go into one of those four options.
Or B - is it when you actually shut down the computer?
If it's the later, Power Manager>General - - Buttons has five options, one being
Ask.
This is obvious, but…. HTH
Re: I would like a warning before shutting down the computer
Posted: Fri Jun 02, 2023 12:46 pm
by Wallon
Dear @j2mcgreg
I know there's a little line above the icon but I don't see it all the time!
It's better to have a warning message before you shut down the computer.
Best regards,
Wallon
Re: I would like a warning before shutting down the computer
Posted: Fri Jun 02, 2023 12:49 pm
by Wallon
Dear @MXRobo ,
I can't make a printscreen of the window with the 5 options.
I would like a message before clicking on the red icon (turn off the computer).
Best regards,
Wallon
Re: I would like a warning before shutting down the computer
Posted: Fri Jun 02, 2023 4:16 pm
by CharlesV
So, I have not had too much testing on this.. but here is a solution that appears to work.
1) Save the below script code to a file named: xfce4-session-logout
2) Copy this file to /usr/local/bin/
3) Make the file executable
When you click on the Menu Logout button you will have a second menu show up, asking what you want to do. Choose Shutdown and it will check for both other logged on users and then open files and attempt to close them gracefully. ( I can remove the other users part if you like, but I like that in this script.)
For me, when I run this with an open, non saved libre office document, it stops the shutdown and then libre office requests the 'Discard, cancel or Save Dialog'. Also other apps that have 'non saved' work.
Note: this does NOT work using the Log out Action Button in the main panel.. I am hunting down how to do that now, but it is being elusive and I am out of time for this today.
I will keep working on this as I can get time, I see it as an interesting addition that is smart!
The script:
xfce4-session-logout
Code: Select all
#!/bin/bash
# requires wmctrl and yad packages
# if dialog already displayed, exit
wmctrl -l | grep "Xfce Exit" && exit 1
close_apps () {
WIN_IDs=$(wmctrl -l | grep -vwE "Desktop$|xfce4-panel$" | cut -f1 -d' ')
for i in $WIN_IDs; do wmctrl -ic "$i"; done
# Keep checking and waiting until all windows are closed
while [ "$WIN_IDs" != "" ]; do
sleep 0.1;
WIN_IDs=$(wmctrl -l | grep -vwE "Desktop$|xfce4-panel$" | cut -f1 -d' ')
done
}
check_if_other_users_logged_in () {
# check if anyone other than $USER is logged in
U=$(loginctl --no-legend list-users | awk '{print $2}' | grep -v $USER)
# if yes, ask if to continue
if [ "$U" != "" ]; then
ans=$(yad --window-icon=system-devices-panel-information --on-top --sticky --fixed --center --title "Xfce Exit" --text-align=center --buttons-layout=center --title "Confirm" --text " The following users are currently logged in: \n\n$U\n\nContinue with action?")
ret=$?
[[ $ret -eq 1 ]] && exit 1 #if cancel, exit
fi
}
# display dialog asking for exit/suspend/lok options
ans=$(yad --window-icon=system-devices-panel-information --on-top --sticky --fixed --center --width 200 --entry --title "Xfce Exit" --text "\nSelect an action:\n" --image=xfce4_xicon3 --image-on-top --button="gtk-ok:0" --button="gtk-cancel:1" --text-align center --entry-text "Logout" "Reboot" "Suspend" "Hibernate" "Power Off" "Lock Screen")
ret=$?
[[ $ret -eq 1 ]] && exit 1 #if cancel, exit
case $ans in
Logout*)
check_if_other_users_logged_in
close_apps
/usr/bin/xfce4-session-logout -l
;;
Power*)
check_if_other_users_logged_in
close_apps
/usr/bin/xfce4-session-logout -h
;;
Reboot*)
check_if_other_users_logged_in
close_apps
/usr/bin/xfce4-session-logout -r
;;
Suspend*)
/usr/bin/xfce4-session-logout -s
;;
Hibernate*)
/usr/bin/xfce4-session-logout --hibernate
;;
Lock*)
xflock4
;;
*) ;;
esac
exit 0
Re: I would like a warning before shutting down the computer
Posted: Fri Jun 02, 2023 5:00 pm
by Jakob77
Wallon wrote: Fri Jun 02, 2023 12:46 pm
It's better to have a warning message before you shut down the computer.
As I see it it all depends on how you turn off the computer.
If you can't remember to close an open document how can anyone relay on it is not the same with the computer so the warning has to be where the wire is plugged in.?
However if you actually are able to follow a close down procedure by using only one icon for it, a reminder is not a problem.
For a while to educate yourself you can just create a panel icon you always use and let that icon open a document (my_dummy_close_down_list.txt) with featherpad.
And in that document you write a good long warning message to yourself.

Re: I would like a warning before shutting down the computer
Posted: Sat Jun 03, 2023 2:10 am
by Duliwi
CharlesV wrote: Fri Jun 02, 2023 4:16 pm
So, I have not had too much testing on this.. but here is a solution that appears to work.
Cool Script. Is there a way to place this file in home? And if yes, in which path?
Edit: Works not, when it is placed here:
~/.bin/xfce4-session-logout
Re: I would like a warning before shutting down the computer
Posted: Sat Jun 03, 2023 11:32 am
by CharlesV
All calls in the script are either explicit or using the path, so it should be able to *run* from anywhere... HOWEVER... the xfce4 shutdown process specifically looks for *** that file name, in that folder *** ( ie /usr/local/bin/xfce4-session-logout ) and if found then it runs it.
SO... I believe you could put the script anyplace and name it differently, ( say Shutdown_Gracefully ) .. and then create a script to call that, and make that script the /usr/local/bin/xfce4-session-logout .
Also, I believe you could set the "graceful" script anyplace and then create a link to it and name that link xfce4-session-logout and put in the /usr/local/bin folder.
What envision doing to make this all tight, is to remove the Log Out Action Button, (unless I can find a method to rework it!), and then create a new "Log Out" button that points to that script. if you use one of the above methods for the menu part, then I think this will all be 'seamless'
When I get a little time I will see about reworking that YAD menu into maybe a GTKDialog and clean it up even more.
Re: I would like a warning before shutting down the computer
Posted: Sat Jun 03, 2023 1:37 pm
by MXRobo
Off Topic - Congrats CharlesV!
Re: I would like a warning before shutting down the computer
Posted: Sat Jun 03, 2023 1:41 pm
by CharlesV
@MXRobo Thank you sir!!
Re: I would like a warning before shutting down the computer
Posted: Wed Oct 04, 2023 1:47 pm
by Jakob77
CharlesV wrote: Fri Jun 02, 2023 4:16 pm
So, I have not had too much testing on this.. but here is a solution that appears to work.
1) Save the below script code to a file named: xfce4-session-logout
2) Copy this file to /usr/local/bin/
3) Make the file executable
When you click on the Menu Logout button you will have a second menu show up, asking what you want to do. Choose Shutdown and it will check for both other logged on users and then open files and attempt to close them gracefully. ( I can remove the other users part if you like, but I like that in this script.)
For me, when I run this with an open, non saved libre office document, it stops the shutdown and then libre office requests the 'Discard, cancel or Save Dialog'. Also other apps that have 'non saved' work.
Note: this does NOT work using the Log out Action Button in the main panel.. I am hunting down how to do that now, but it is being elusive and I am out of time for this today.
I will keep working on this as I can get time, I see it as an interesting addition that is smart!
The script:
xfce4-session-logout
Code: Select all
#!/bin/bash
# requires wmctrl and yad packages
# if dialog already displayed, exit
wmctrl -l | grep "Xfce Exit" && exit 1
close_apps () {
WIN_IDs=$(wmctrl -l | grep -vwE "Desktop$|xfce4-panel$" | cut -f1 -d' ')
for i in $WIN_IDs; do wmctrl -ic "$i"; done
# Keep checking and waiting until all windows are closed
while [ "$WIN_IDs" != "" ]; do
sleep 0.1;
WIN_IDs=$(wmctrl -l | grep -vwE "Desktop$|xfce4-panel$" | cut -f1 -d' ')
done
}
check_if_other_users_logged_in () {
# check if anyone other than $USER is logged in
U=$(loginctl --no-legend list-users | awk '{print $2}' | grep -v $USER)
# if yes, ask if to continue
if [ "$U" != "" ]; then
ans=$(yad --window-icon=system-devices-panel-information --on-top --sticky --fixed --center --title "Xfce Exit" --text-align=center --buttons-layout=center --title "Confirm" --text " The following users are currently logged in: \n\n$U\n\nContinue with action?")
ret=$?
[[ $ret -eq 1 ]] && exit 1 #if cancel, exit
fi
}
# display dialog asking for exit/suspend/lok options
ans=$(yad --window-icon=system-devices-panel-information --on-top --sticky --fixed --center --width 200 --entry --title "Xfce Exit" --text "\nSelect an action:\n" --image=xfce4_xicon3 --image-on-top --button="gtk-ok:0" --button="gtk-cancel:1" --text-align center --entry-text "Logout" "Reboot" "Suspend" "Hibernate" "Power Off" "Lock Screen")
ret=$?
[[ $ret -eq 1 ]] && exit 1 #if cancel, exit
case $ans in
Logout*)
check_if_other_users_logged_in
close_apps
/usr/bin/xfce4-session-logout -l
;;
Power*)
check_if_other_users_logged_in
close_apps
/usr/bin/xfce4-session-logout -h
;;
Reboot*)
check_if_other_users_logged_in
close_apps
/usr/bin/xfce4-session-logout -r
;;
Suspend*)
/usr/bin/xfce4-session-logout -s
;;
Hibernate*)
/usr/bin/xfce4-session-logout --hibernate
;;
Lock*)
xflock4
;;
*) ;;
esac
exit 0
I love when you bring superior scripts like this to forum. Keep up the good work.

If you want my help to test it I can go in action.
I have made launchers that can shut down, reboot or log off just by one click.
However there is a program or two I want to protect so it won't work if they are open.
And now I think I have some simple code that can do the job from a script in ~/bin
I am still testing and waiting for someone to confirm the code or bring better (here for conditioned turning off network) :
viewtopic.php?p=746079#p746079
And then a script that will prevent the shut down icon from working will go something like this for Thunderbird, Featherpad and Libre-Office:
Code: Select all
#!/bin/bash
# This script is for shutting down the computer only if Thunderbird, Featherpad and Libre Office are not running
# Conditions: "~/bin" must exist ; Creates: ~/bin/Open-prog-indicator.log
ps -ef > ~/bin/Open-prog-indicator.log
if grep -q "thunderbird" "$HOME/bin/Open-prog-indicator.log" ; then
echo 'You must close Thunderbird and other programs first' ;
else
if grep -q "featherpad" "$HOME/bin/Open-prog-indicator.log" ; then
echo 'You must close Featherpad and other programs first' ;
else
if grep -q "libre" "$HOME/bin/Open-prog-indicator.log" ; then
echo 'You must close Libre-Office and other programs first' ;
else
echo 'Done testing. - Disable this line in the script and enable the one below to make shut down work.' ;
# xfce4-session-logout --halt --fast ;
fi
fi
fi
I wanted grep to look for several strings at the same time but I couldn't make it work.
Wonder how many fi's I can add. ;-)
Duliwi
What do you think. Does the script look okay to you.?
Wallon
I guess you have learnt the close down procedure the hard way by now.
I just remembered your question and wanted to let you know that something is still going on. :-)
Re: I would like a warning before shutting down the computer
Posted: Wed Oct 04, 2023 1:59 pm
by CharlesV
Thanks.
On the other topic... In my opinion ... anything that uses a file based test to determine if a program is open / running etc.. is a kludge and should not be used unless there is no other way. YEARS of programming network applications have proven that you can get into trouble if you rely on things like this and MUST use some workarounds to make sure things are indeed still alive. ( or you must be blind assumptions - which WILL get you into trouble at one point or another. )
For linux, to determine if a program is REALLY still working... the pid is the only real thing you can rely on - and... almost any application can infact still have a pid and be pretty much a zombie and no longer operational.
And for networking... there is only one way. Testing if you have connectivity - at that moment - that you need to know. Either router based, or internet based, etc - but if you dont have some check here... you *will* get into a failure of your test!
Re: I would like a warning before shutting down the computer
Posted: Thu Oct 05, 2023 5:17 pm
by Jakob77
Okay, so there is still a need for more testing and confirmation.
If it brings an illness to the computer I am sure I will be in trouble because I am implementing it.
And during the work I figured out that the script I want is pretty compatible for sharing with others who might also want to test.
So this is the version I am using:
Code: Select all
#!/bin/bash
# This script is for taking action and execute command only if the defined programs are not running
# Creates ~/bin/Open-prog-indicator.log
# ~/bin must exist and this script is made for that folder.
# PATH to ~/bin is supposed to exist: https://forum.mxlinux.org/viewtopic.php?p=724839#p724839
# Development: https://forum.mxlinux.org/viewtopic.php?p=746869#p746869
# Main sequence begin
case $1 in
# Sequence begin
# If none of the mentioned programs are running then "$2" will be executed.
'down'|'logout'|'reboot')
ps -ef > ~/bin/Open-prog-indicator.log
if grep -q "thunderbird" "$HOME/bin/Open-prog-indicator.log" ; then
echo 'You must close Thunderbird and other programs first' ;
else
if grep -q "feather" "$HOME/bin/Open-prog-indicator.log" ; then
echo "You must close FeatherPad and other programs first" ;
else
if grep -q "libre" "$HOME/bin/Open-prog-indicator.log" ; then
echo "You must close Libre-Office and other programs first" ;
else
if grep -q "firefox" "$HOME/bin/Open-prog-indicator.log" ; then
echo "You are supposed to close Firefox and other programs first" ;
else
$2 ;
fi
fi
fi
fi
;;
# Sequence end
# Sequence begin
'-h'|'help'|'-help'|'-?'|'/h'|'/?'|'/help'|'--help'|'none')
clear
echo "This script built to be used directly or as a subscript to a session script"
echo "It makes sure to execute Command only if none of the defined programs are running"
echo "Use: ['${0##*/}'] [Argument] [Command] - Allow or prevent the Command from being executed."
echo ""
echo "Arguments:"
echo ""
echo " none A tip for help"
echo " down , logout , reboot They all do the same before executing Command $2."
echo " Checks that some programs are closed before Command is executed."
echo ""
echo "Eksample:"
echo ""
echo ""'${0##*/}' down dir" Runs dir if none of the scripted programs are open"
echo ""
;;
# Sequence end
# Sequence begin
*)
echo " Write "'${0##*/}' --help" to get help for the command"
;;
# Sequence end
esac
# Main sequence end
I give the script a name (protectprograms.sh), put it in ~/bin and give it permission to run as a program.
I have ~/bin in my PATH so after that I can just make launchers on the desktop for these commands to do the jobs:
Code: Select all
protectprograms.sh logout 'xfce4-session-logout --logout --fast'
Code: Select all
protectprograms.sh reboot 'xfce4-session-logout --reboot --fast'
Code: Select all
protectprograms.sh down 'xfce4-session-logout --halt --fast'
I don't know about the ' --fast' option. I am looking for a good reason to take it away.
Also I have not made the Whisker integration yet, and I don't know if I want to, or how difficult it is.
But the script is now running hopefully to provide us with one more possible solution. :-)
Edit: In order to remove some danglish and correcting code accordingly to Wallons comment below the script has been changed a bit, and it has not been tested afterwards.
There can be more you want to edit to make it fit your specific needs. It is still far from French. lol - Also the warning can not be seen if the script is executed from desktop. I don't think it is needed but it is not difficult to make. It is just a question of using something else instead of echo.
Re: I would like a warning before shutting down the computer
Posted: Fri Oct 06, 2023 12:41 pm
by Wallon
Dear
@Jakob77,
You need to change your echo command. The apostrophe is a big problem for some languages like French.
Try typing the following echo command in a terminal and the message will not be displayed.
Code: Select all
echo 'L'apostrophe is a problem for french if it is used with the command echo to display a message'
You need to display a message with English inverted commas to avoid problems with the apostrophe. Here is a good example;
Code: Select all
echo "L'apostrophe is a problem for french if it is used with the command echo to display a message"
Kind regards,
Wallon