Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
) &
share|improve this question
    
OP are you still looking for an answer? If so, you may need to repost your question as this is flagged for closure. Regards, –  Ringtail Mar 28 '12 at 16:19

1 Answer 1

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
) &
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.