Confirm to shut down, reboot or log out in Openbox

December 3, 2007

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 (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:

gmessage

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.

28 Responses to “Confirm to shut down, reboot or log out in Openbox”

  1. Great post, very helpful! Is there a way to make gmessage show the message as a label instead of an input box?

  2. Nice way to get shut down sorted.

    I can see many good uses for gmessage, in a similar way to using Zenity

    Had to view your blog with no page style in order to read all the “coded” text. Not sure if this is the same for others.

  3. urukrama said

    I know that the code doesn’t show completely, but I don’t know how to get around that. If you select the text and paste it into a new document you should have the entire code, though. It isn’t perfect, but it works.

    If you (or anyone else) knows how to fix this, please let me know.

    • Hello-you said

      This is 7 years late, but if you add ‘overflow: auto;’ into your css file, it will automatically add scroll bars if necessary. So if the ‘coded’ text had the class ‘code,’ you would put something like: ‘.code {overflow:auto;}’ into your css file.

  4. If you can edit your css file, look for widths in header and page to make things a bit wider. Try @ 760px or put soft returns in?

    You are right, though selecting text does grab it all, but printing to pdf only captures the on screen info. (I like to pdf good stuff like this 🙂 )

  5. Gary said

    I’m trying to get this same thing done, but “killall openbox” only refreshes openbox. it doesn’t logout.

  6. urukrama said

    Gary, with current versions of Openbox you can now use the command “openbox –exit” instead of “killall openbox”. Does that work?

  7. Gary said

    Thanks. that worked in the end.

    However, there seems to be a new problem. After migrating to Arch Linux, when I try to get the gmessage box to appear, nothing happens. I’ve followed all the instructions, but still nothing. gmessage for me is in “/usr/local/bin/”, does that affect anything?

  8. chris4585 said

    Thank you! 🙂

  9. Marco said

    Hi,
    now I’m using Openbox 3.4.7.2 on Debian, and I found another way to display a confirm box using sudo and Openbox Execute Action options “Prompt” (http://icculus.org/openbox/index.php/Help:Actions#Execute). This is part of my menu.xlm:

    Are you sure you want to reboot the machine?
    sudo shutdown -r now

    Are you sure you want to shut down the machine?
    sudo shutdown -h now

    In this way, you are prompt to choose Yes if you want to shut down or reboot and the window has the same theme of your desktop… Only downside is the title of the window: “Execute” and I don’t know how to change it…

  10. Marco said

    Hi,
    now I’m using Openbox 3.4.7.2 on Debian, and I found another way to display a confirm box using sudo and Openbox Execute Action options “Prompt” (http://icculus.org/openbox/index.php/Help:Actions#Execute). This is part of my menu.xlm:

    Are you sure you want to reboot the machine?
    sudo shutdown -r now

    Are you sure you want to shut down the machine?
    sudo shutdown -h now

    In this way, you are prompt to choose Yes if you want to shut down or reboot and the window has the same theme of your desktop… Only downside is the title of the window: “Execute” and I don’t know how to change it…

  11. Marco said

    Apologies, I’m not able to post my xml code ..

  12. michel said

    You can do the same with an menu.xml entry. By adding:

    like:

    insert text that you will see when you get the yes or no prompt

    Example for reboot:

    Are you sure you want to reboot!?

    sudo shutdown -r now <— NO QUOTES!!!

  13. michel said

    Yup the xml code goes away. Lets try this way.

    Example for reboot:

    item label=”reboot”
    action name=”Excute”
    prompt
    Are you sure you want to reboot!?
    /prompt
    execute
    sudo shutdown -r now <— NO QUOTES!!!
    /execute
    /action
    /item

    If it shows, just need to add the where they go.

  14. Marco said

    Yes Michel, you’re right !!! I’m using the same menu entry, but I was not able to publish it…

  15. p0ft said

    Thanks. I’ve setup it correctly and it’s running well. Now my parents can easily shutdown my linux/openbox.

  16. I hope you don’t do this on a multiuser system. The edit you made to the sudoers file will allow anyone to shut down the computer without a password. If you allow remote users, this means that they can just reboot your computer regardless of what you’re doing!
    This line:
    username hostname = NOPASSWD: /sbin/shutdown
    will limit the power to just one user, instead of using ALL ALL. Better? I think so.

  17. Dirk said

    Skip this sudo crap and use dbus!

    dbus-send –system –print-reply \
    –dest=”org.freedesktop.Hal” \
    /org/freedesktop/Hal/devices/computer \
    org.freedesktop.Hal.Device.SystemPowerManagement.Shutdown

    (yes, you need to remove the slashes and newlines)

    • Marco said

      Hi,
      now HAL is deprecated, so you can use DeviceKit and UPower to halt, reboot, suspend and hibernate your pc. On debian, just install devicekit, policykit and upower, and then use these commands on your menu.xml:

      To suspend:

      dbus-send –system –print-reply \ –dest=org.freedesktop.UPower \
      /org/freedesktop/UPower \
      org.freedesktop.UPower.Suspend

      To hibernate:

      dbus-send –system –print-reply \ –dest=org.freedesktop.UPower \
      /org/freedesktop/UPower \ org.freedesktop.UPower.Hibernate

      To reboot:

      dbus-send –system –print-reply \ –dest=org.freedesktop.ConsoleKit \
      /org/freedesktop/ConsoleKit/Manager \ org.freedesktop.ConsoleKit.Manager.Restart

      To halt:

      dbus-send –system –print-reply \ –dest=org.freedesktop.ConsoleKit \
      /org/freedesktop/ConsoleKit/Manager \ org.freedesktop.ConsoleKit.Manager.Stop

      !!!!! remove slashes and newlines !!!!

  18. jrn23 said

    thank you for the help you provided with this tip…the problem with me is that when i press L,R or S it doesn’t take any action…i must use the TAB key to go the desired button and press ENTER for the desired action to take effect…what might be the problem? thanks in advance…

  19. […] script does not work with gdm 2.28.0. Instead I followed the instructions here. In order to minimize the number of files that would need to be modified, I overwrote the existing […]

  20. Patrick said

    For Arch users, gmessage does not appear to be there. Instead it is called gxmessage.

  21. Tom said

    Howto set the logout button to go to xdm?

    without “Logout+Shutdown…”Panel?

    at moment i must remove hal+dbus

    but there must be a other way on sqeeze

  22. the dsc said

    Perhaps it didn’t exist back in 2007, but these days you can use pure openbox to have a shutdown dialog:

    sudo shutdown -h now

    Are you sure you want to * shutdown * the PC?

    And yes, that’s just the way it is, the “prompt” comes after the “command”, even if it’s a bit odd.

    Perhaps it’s even possible to have a more complex dialog with the options to shutdown, reboot, log out, switch user or cancel, like some do with “oblogout”, but rendered solely by openbox, but I achieve the same with submenus (“shutdown” for reboot and shutdown, and “session” for log out, lock, and switch user) and these specific confirmation dialogs. I even prefer that way as it reduces to zero the odds of rare but not nonexistent muscular spasm make me click on reboot when I wanted to shutdown or vice-versa, or worse, when I intended to switch the user or lock the screen.

  23. the dsc said

    the xml tags were deleted by the blog system. Here they go again:

    (item label=”shutdown”)
    (action name=”Execute”)
    (command)
    sudo shutdown -h now
    (/command)
    (prompt)
    Are you sure you want to * shutdown * the PC?
    (/prompt)
    (/action)
    (/item)

  24. […] reconocimiento al genial Blog de Urukrama por la idea 🙂 MeneameDivúlgameDivobloggerBitacorasRedditDeliciousGoogle BuzzFacebookTwitter […]

  25. sudo -less shutdown/restart via dbus:

    dbus-send –print-reply –system –dest=org.freedesktop.Hal /org/freedesktop/Hal/devices/computer org.freedesktop.Hal.Device.SystemPowerManagement.Shutdown

    dbus-send –system –print-reply –dest=org.freedesktop.Hal /org/freedesktop/Hal/devices/computer org.freedesktop.Hal.Device.SystemPowerManagement.Reboot

  26. dencrypt said

    You could use “openbox –exit” instead of killall.

Leave a reply to Jose Catre-Vandis Cancel reply