Instead, I would like to use the power button to issue the shutdown command. After some research, found that the power button generates events that can be captured by monitoring /dev/input/event0.
Here is the script:
#!/bin/sh BTN=/dev/input/event0 while true; do BTNVAL=`hexdump -e '8/2 "%x " "n"' -n 16 $BTN | grep ' 74 ' | awk '{print $7}'` if [ "$BTNVAL" = "1" ] then echo "Power button pressed" /bin/sync; /bin/sync; /sbin/shutdown -h now exit 0 fi sleep 1 done
The script reads the first 16 bytes and see if it contains 0x74, which is the code for power button. If so, it will execute the shutdown command. Otherwise, sleep for awhile and monitor again.
The script is add to /etc/rc.local in order to be executed after every reboot
/root/scripts/pwrbtncheck.sh > /dev/null 2>&1 &