Confirm to shut down, reboot or log out in Openbox
I am clumsy and at times inattentive. Especially after a hard day’s work, I sometimes perform actions on my computer without giving them much thought. I close an application, whereas I only wanted to close the current page; I accidentally save older versions of files and overwrite my latest version in the process; or I reboot my computer when I wanted to log out or shut down. Sometimes my mouse slips and accidentally clicks “shut down” in the Openbox menu, allowing me to see all my unsaved OpenOffice documents disappear before me. I’m sure I’m not the only one who has such moments (or at least I hope so ).
If possible I therefore want the computer to ask for a confirmation before I do something drastic. However much I love Openbox, I always missed a dialog box asking me to confirm whether I wanted to log out or shut down. Here is how you can create one.
Using gmessage, gxmessage (or xmessage if you like things ugly), you can create a dialog box that follows your gtk theme and asks you what action you want to take. Here is what mine looks like:
So how do you do it? First install gmessage:
sudo aptitude install gmessage
Next create a script that looks a little like the one below (adapted from here):
#!/bin/bash
gmessage "Are you sure you want to shut down your computer?" -center -title "Take action" -font "Sans bold 10" -default "Cancel" -buttons "_Cancel":1,"_Log out":2,"_Reboot":3,"_Shut down":4 >/dev/null
case $? in
1)
echo "Exit";;
2)
killall openbox;;
3)
sudo shutdown -r now;;
4)
sudo shutdown -h now;;
esac
This gives the message you’ve seen above, centred on the screen. Cancel is the default button, pressing esc will close the window, pressing S will shut down, R reboot, L log out of Openbox. (I am not too sure if “killall openbox” is the best option to log out of Openbox, but it does the trick. If you know of a better way, please let me know.)
Change the above to suit your preferences. Have a look at “man gmessage” for more options. Save the script somewhere (I keep all my scripts in ~/.scripts/), and make the file executable:
chmod a+x /path/to/script
Now you’ll want to add the script to your menu or create a keyboard binding for it in the rc.xml file. This is done like you would for any application, except that the command here is the path to the script, in my case /home/urukrama/.scripts/shutdown.
If you want to make the message box to display without any window decorations, either add the parameter “-borderless” to the above script (somewhere in the first part, for example after “-center“) or add the following to your rc.xml file in the applications section (at the end of the file):
<application name="gmessage">
<decor>no</decor>
<shade>no</shade>
<skip_pager>yes</skip_pager>
<skip_taskbar>yes</skip_taskbar>
<fullscreen>no</fullscreen>
<maximized>no</maximized>
</application>
This method work best if you allow users to shut down and reboot without administrative privileges, but will work also if you don’t — just replace all the “sudo”-s with “gksudo” in the script and it will ask you for your password before it shuts down.
To allow users to shut down without administrative privileges, do the following: Open a terminal and type
sudo visudo
Add the following at the bottom of the page:
ALL ALL=NOPASSWD:/sbin/shutdown
Save and exit, and you won’t be needing your password to reboot or shut down. (If you prefer to use the “halt” command to shut down and reboot, replace the above line with ALL ALL=NOPASSWD:/sbin/halt.)
That’s it. The chances that you accidentally shut down or log out are now a bit smaller.








