I would like a warning before shutting down the computer

Message
Author
MXRobo
Posts: 1838
Joined: Thu Nov 14, 2019 12:09 pm

Re: I would like a warning before shutting down the computer

#11 Post by MXRobo »

Off Topic - Congrats CharlesV!

User avatar
CharlesV
Administrator
Posts: 7888
Joined: Sun Jul 07, 2019 5:11 pm

Re: I would like a warning before shutting down the computer

#12 Post by CharlesV »

@MXRobo Thank you sir!!
*QSI = Quick System Info from menu (Copy for Forum)
*MXPI = MX Package Installer
*Please check the solved checkbox on the post that solved it.
*Linux -This is the way!

Jakob77
Posts: 658
Joined: Thu Feb 09, 2023 3:09 am

Re: I would like a warning before shutting down the computer

#13 Post 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. :thumbup:
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. :-)

User avatar
CharlesV
Administrator
Posts: 7888
Joined: Sun Jul 07, 2019 5:11 pm

Re: I would like a warning before shutting down the computer

#14 Post 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!
*QSI = Quick System Info from menu (Copy for Forum)
*MXPI = MX Package Installer
*Please check the solved checkbox on the post that solved it.
*Linux -This is the way!

Jakob77
Posts: 658
Joined: Thu Feb 09, 2023 3:09 am

Re: I would like a warning before shutting down the computer

#15 Post 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.
Last edited by Jakob77 on Fri Oct 06, 2023 7:20 pm, edited 1 time in total.

Wallon
Posts: 339
Joined: Wed Oct 21, 2020 4:08 pm

Re: I would like a warning before shutting down the computer

#16 Post 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

Post Reply

Return to “Software / Configuration”