1
\$\begingroup\$

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
 else
 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
 exit
 fi
 shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value)
 done
 if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMINIMUM ]; then 
 echo "X735 Rebooting", SHUTDOWN, ", recycling Rpi ..."
 sudo reboot
 exit
 fi
 fi

Thank you.

asked May 18, 2021 at 14:54
\$\endgroup\$
3
  • 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\$ 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\$ 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\$ Commented May 19, 2021 at 18:47

1 Answer 1

1
\$\begingroup\$

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).

answered May 18, 2021 at 15:48
\$\endgroup\$
2
  • \$\begingroup\$ Thank you Toby, it turns out that "watch" command works well with /sys folder. \$\endgroup\$ 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

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.