Saturday, September 14, 2013

Using the Power Button on Beaglebone Black for Shutdown

By default, when pressing the Power button on Beaglebone Black (BBB) for 8 seconds, the board will be powered off. However, this is not properly shutting down the OS.

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:



while true; do
  BTNVAL=`hexdump -e '8/2 "%x " "n"' -n 16 $BTN | grep ' 74 ' | awk '{print $7}'`
  if [ "$BTNVAL" = "1" ]
    echo "Power button pressed"
    /bin/sync; /bin/sync; /sbin/shutdown -h now
    exit 0
  sleep 1

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/ > /dev/null 2>&1 &


pingpong said...

on my system it seems not to work.
execution on the shell of:
hexdump -e '8/2 "%x " "n"' -n 16 $BTN | grep ' 74 ' | awk '{print $7}'

does not return something, it waits for some input ?!
same with:
cat /dev/input/event0

do you have a hint?

clarenceho said...

It waits for the button pressed event. Try to tail the event0 device. See if any event is generated when u press the button

Ati said...

Hi is there a typo at "/bin/sysnc;"?


clarence said...

@Ati: you are correct. Typo corrected