How often do you use power button to shut down your computer? I never use it, and, even if I would have need to shutdown or restart my computer, I would use GUI way to do it (simply because, it is something that is rarely used, I never remember I could use power button in that split second when I want to shut it down)

But, if you (like me) don’t have keyboards with fancy shortcut buttons, why not use that same power button for some shortcut. Or, you can make fun of your Windows friends just by showing what power and mind-boggling functionality Linux have and just waits for yours ideas to unleash it. I think this is great example of that.

I will use Ubuntu to set it up, but, with minor (or even none) modification, this should work on any distro. First you will need, is to remove annoying gnome log-off screen that shows up when you press power button. For this, we will need to change two keys in gconf. Start gconf-editor and change those two keys:

  • /apps/gnome-power-manager/action_button_power
  • /apps/gnome-power-manager/buttons/power

to “nothing” (without quotes). This will disable log-off screen. Now, it’s all about editing /etc/acpi/powerbtn.sh to suit your needs. I will give you two possible scenarios, and there are no limits what you can do.

Enable/disable internet (forwarding)

I already had two little script to enable or disable internet to some users I am routing internet to. These are some iptables stuff (something like `iptables -I FORWARD -p ALL -s 192.168.0.122 -j DROP`) and I had to open console, execute it and later enable it. This can be done with this in /etc/acpi/powerbtn.sh:

#! /bin/sh
if [ -f /var/tmp/internet_off ]; then
    /etc/rc.start_internet
    rm /var/tmp/internet_off
else
    /etc/rc.stop_internet
    touch /var/tmp/internet_off
fi

This is stupid script. It checks for existence of file /var/tmp/internet_off that signalizes if forwarding is on. It then executes appropriate scripts. If you have someone dumb enough, you can tell them that they have no internet because your computer is turned off, and what they see on monitor is just a picture, or drop them a story about virtual (wau!) machine that run even when computer is off (everyone heard of vmware) – something like that:) You can even confirm this by pressing button in front of them to enable or disable their internet access.

Starting firefox

I use firefox all day. If I ever want to dedicate any button to any program, that is certainly firefox. But to start GUI program in shell script that is run as root, we need some workarounds. For example, you need to set DISPLAY and XAUTHORITY environment variable.

#! /bin/sh
export DISPLAY=:0
export XAUTHORITY=/home/your_username/.Xauthority
sudo -u your_username firefox

But, this script have few defects. If firefox is running, it will launch another windows, and we see ‘yourusername’ is all around. This is improved version:

#! /bin/sh
USERNAME=your_username
export DISPLAY=:0
export XAUTHORITY=/home/$USERNAME/.Xauthority
 
if ps -U $USERNAME -u $USERNAME|grep firefox; then
    sudo -u $USERNAME firefox -new-tab about:blank
else
    sudo -u $USERNAME firefox
fi

If firefox is running, it will just open new empty tab, otherwise it will launch it normally. Also, your username is pulled up as variable, so you just need to change second line to your username and this script will work. Have fun!

Further ideas

Only one function for button is nothing. Maybe we can split two different functionality by defining click and double-click on power button. Simple creating file on first click, and checking if file was created 2 seconds before current time (I will not go into details here). Also, you could use that button to open CD tray, why not:) Like anything in Linux, your creativity is your only limit. Do you have any ideas what to do else with this button? What would you put in /etc/acpi/powerbtn.sh?

, , ,