Ubuntu – Customize the power button action (ubuntu 10.04)

acpipower-managementshutdown

I would like to customize the power button action. The tutorial at http://blog.metalight.dk/2010/07/ubuntu-lucid-custom-power-button-event/ works but not always (the number of shutdown processes can sometimes be different).

I need the solution where

  • 1x press of a button executes the usual OS shutdown
  • 3x press of a button executes OS reboot

How would you write the code for it?
FILE: /etc/acpi/powerbtn.sh

#!/bin/sh
# /etc/acpi/powerbtn.sh
# Initiates a shutdown when the power putton has been
# pressed.

# you need to double 'click' the power button to shutdown
( if ! [ $(pidof -x powerbtn.sh | wc -w) -eq 3 ]; then
    sleep .4
    exit
  else
    poweroff
  fi
) &

Best Answer

The number of processes should not be different. You have to remember when counting the pids to include the script itself and the subprocess created.

I created the following that should work for your requirements. You should alter the timeout depending on how quickly you want the multiple button presses.

#!/bin/sh
# /etc/acpi/powerbtn.sh
# Initiates a shutdown when the power putton has been
# pressed.

timeout=0.8
pid_count=$(pidof -x powerbtn.sh | wc -w)

( if [ $pid_count -eq 4 ]; then
        sleep $timeout
        /etc/acpi/sleep.sh
    else
        sleep $timeout
        pid_count_now=$(pidof -x powerbtn.sh | wc -w)
        if [ $pid_count_now -eq 2 ] && [ $pid_count -eq 2 ]; then
            poweroff
        fi
        exit
    fi
) &