The power button is pretty boring just turning the computer off.
Time to make it special for those headless Linux machines that play music but don’t have a keyboard.
Make sure ACPI is installed.
~$ sudo apt-get install acpi
Backup the old script.
~$ sudo cp /etc/acpi/powerbtn.sh /etc/acpi/powerbtn.sh.bak
Clear all of that nonsense out of the old script and start with a fresh slate.
~$ sudo cat > /etc/acpi/powerbtn.sh
#!/bin/bash
/path/to/some/cool/programorscript.sh
Hit ctrl-c to finish
But that only lets you do one thing with your button!
Lets fix that with a little evil and trickery. The following will wait for repeated presses after the first for different results.
Replace /etc/acpi/powerbtn.sh with this.
#!/bin/bash
killall mainpowerbutton.sh
/root/mainpowerbutton.sh &
Now create /root/mainpowerbutton.sh and place this in it.
#!/bin/bash
echo “i’ve been touched” >> /root/scriptisrunning
sleep 3
if [ `cat /root/scriptisrunning | wc -l` -eq 1 ]; then
/path/to/desired/program/after/1/press
fi
if [ `cat /root/scriptisrunning | wc -l` -eq 2 ]; then
/path/to/desired/program/after/2/presses
fi
if [ `cat /root/scriptisrunning | wc -l` -eq 3 ]; then
/path/to/desired/program/after/3/presses
fi
rm /root/scriptisrunning
exit
This allows you to press the button multiple times with different results for each press. The sleep timer will reset with each press so this can be expanded to any number of button presses. Just copy the if statement and change the -eq to the number of presses. Then put the path to the program or script you want to run on the next line. Make sure the if statement has a corresponding fi to end it.