≡ Menu

How To Add Shutdown / Reboot Functionality to Fluxbox Window Manager for X

Question: How do I add shutdown / Reboot functionality to the fluxbox window manager? Can you explain to me with clear examples?

Answer: In our previous article we gave an introduction to the fluxbox. In this article, let us review how to add shutdown or reboot functionality to fluxbox.

Adding Shutdown / Reboot Functionality to Fluxbox

You don’t need a separate program to shutdown or reboot your PC. In Linux and UNIX it can be accomplished with a simple BASH script, the xmessage program (built into xorg), and modifying the sudoers file. The sudoers file dictates which users may execute what programs. You can learn more about it by reading the sudoers man page (man sudoers).

In this tutorial, you will modify the sudoers file so that people in an administrative group may execute the shutdown and reboot programs without a password. The entry in the sudoers file would look something like the following.

%staff  ALL=NOPASSWD: /sbin/shutdown,/sbin/reboot

If one were to read this out it would sound like the following. Everyone in the staff group (%staff) may execute the /sbin/shutdown and /sbin/reboot programs without a password.

Now we will write a BASH script that calls the xmessage program. Here is the BASH script that I use:

#!/usr/bin/env BASH
#shutdown script
xmessage "Are you sure you want to shut down your computer?" -center -title "Take action" -default "Cancel" -buttons "Cancel":1,"Reboot":2,"Shutdown":3 

case $? in
    1)
        echo "Exit";;
    2)
        sudo reboot;;
    3)
        sudo shutdown;;
esac

If that looks like a whole lot of gibberish to you then don’t panic. I will explain.

  • The first line tells the operating system what interpreter to use. Since this is a BASH script we will need to use the BASH interpreter.
  • The env (environment) program finds the interpreter. I feel that using the env program makes my scripts more portable because not every operating system is going to store everything under the same directory. However, the env program is always under /usr/bin/.
  • The case statement is just a replacement for using if statements. I find it works great for commandline arguments, which BASH stores as $1, $2, $3….$n. It is basically a replacement for if/then/else statement.
  • Make sure to store this script in ~/bin and name it something like shut_custom so it doesn’t interfere with other executables.
  • If you haven’t added ~/bin to your $PATH then do so now by editing the ~/.bashrc. All you have to do is type PATH=$PATH:/path/to/home/bin.

So far we have our script written and our sudoers modified but now what? Now we edit our menu. I won’t go into deep details about editing the menu. If you did everything properly then the following entry should work just fine.

[exec] (Log Off) {shut_custom}

Finally, don’t forget to replace shut_custom with whatever it is you named the script!

Fig: Shutdown / Reboot pop-up window in Fluxbox WindowManager

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • fishcooker May 31, 2010, 11:40 pm

    change xmessage with zenity for more eye-candy

  • smith matsiko October 14, 2011, 7:33 am

    I had the same challenge, this is how i simplified it quickly..
    First add the user to SudoUsers:
    as root edit the file; #visudo
    Add the following(substitute %users with the user)
    %users ALL= NOPASSWD: /sbin/shutdown
    %users ALL= NOPASSWD: /sbin/reboot

    Now edit ~/.fluxbox/menu(this is a symbolic link to /etc/X11/fluxbox/fluxbox-menu)
    I prefer editing /etc/X11/fluxbox/fluxbox-menu
    Append this line in the menu position of your choice between [begin] (fluxbox) and [end]
    [exec] (Shutdown) {sudo /sbin/shutdown -hP now}

    Enjoy..

  • El Diablo May 23, 2014, 2:22 pm

    #!/usr/bin/env BASH

    ^^ for Debian Fluxbox, change the word “bash” to lowercase