I have installed Geekworm x735 on my Raspberry Pi, it uses the script below with an infinite loop to determine if the power button has been pressed. I know there are some Linux utils that watch for file changes (inotifywait, watch, fswatch to name a few), I wonder if they are more efficient than this solution or if under the hood they apply the same logic.

while [ 1 ]; do
  shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value)
  if [ $shutdownSignal = 0 ]; then
    /bin/sleep 0.2
    pulseStart=$(date +%s%N | cut -b1-13)
    while [ $shutdownSignal = 1 ]; do
      /bin/sleep 0.02
      if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMAXIMUM ]; then
        echo "X735 Shutting down", SHUTDOWN, ", halting Rpi ..."
        sudo poweroff
      shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value)
    if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMINIMUM ]; then 
      echo "X735 Rebooting", SHUTDOWN, ", recycling Rpi ..."
      sudo reboot

Thank you.

  • 1
    \$\begingroup\$ That's not really a code review question. You could find out how they work by looking at the sources. Shortly: They don't work this way but use Linux-specific notification mechanisms which only act on change, they don't permanently poll for changes. \$\endgroup\$
    – uli
    Commented May 18, 2021 at 15:14
  • \$\begingroup\$ Thank you Uli, that's all I wanted to know. I'd like to close the question but apparently I can't? \$\endgroup\$
    – Grumoll
    Commented May 18, 2021 at 15:29
  • 1
    \$\begingroup\$ I have used triggerhappy on other Linux SBCs to handle buttons, wouldn't it be a better option perhaps ? \$\endgroup\$
    – Kate
    Commented May 19, 2021 at 18:47

1 Answer 1


Polling like this is a very inefficient mechanism, but for pseudo-files in /sys/, you may find that the usual kernel interfaces as used by inotifywait and the like don't get notified of changes in the same way that they do for real storage-backed files. So it may be that you need to poll, unfortunately. But do check, for the files you care about.

There doesn't seem to be any value creating shutdownSignal to immediately use - simpler to just insert the value directly:

while [ $(</sys/class/gpio/gpio$SHUTDOWN/value) = 1 ]

sudo is intended for interactive use, and doesn't work well in scripts. I think it's better to assume (or check) that the script is invoked with sufficient capabilities to do the work it needs to do.

Don't forget to add a shebang line (#!/bin/bash).

  • \$\begingroup\$ Thank you Toby, it turns out that "watch" command works well with /sys folder. \$\endgroup\$
    – Grumoll
    Commented May 20, 2021 at 8:45
  • 1
    \$\begingroup\$ @Oriol, watch works by polling, so yes, it will work with files that don't implement notification. \$\endgroup\$ Commented May 20, 2021 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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