Optimizing a `while` loop
Analysis and modern solution
The script is a busy loop: it keeps reading the GPIO pins over and over. It doesn't consume much memory but it keeps the CPU busy.
You should set the GPIO pin in edge mode. The gpio
utility has a wfi
(wait for interrupt) command which you can use to react to an edge trigger. (gpio wfi
didn't exist back when the question was asked.)
set -e
gpio mode 0 in
gpio wfi 0 rising
echo password | sudo -S reboot
A Python solution
There is a Python library for GPIO access, which supports edge mode. Here's some completely untested Python code that should do what you want.
#!/usr/bin/env python
import os
from RPi import GPIO
GPIO.wait_for_edge(0, GPIO.RISING)
system("sudo reboot")
Additional shell tips
(true)
could be written just true
. The parentheses create a subprocess, which is completely unnecessary.
`gpio read 0`
should be in double quotes. Without quotes, the output of the command is treated as a list of file name wildcard patterns. With double quotes, the output of the command is treated as a string. Always put double quotes around command substitutions and variable substitutions: "$(some_command)"
, "$some_variable"
. Also, you should use the syntax $(…)
rather than `…`
: it has exactly the same meaning, but the backquote syntax has some parsing quirks when the command is complex. Thus: if [ "$(gpio read 0)" -eq 1 ]
Don't put the root password in the script. If the script is running as root, you don't need sudo at all. If the script isn't running as root, then give the user running the script the permission to run sudo reboot
without supplying a password. Run visudo
and add the following line:
userwhorunsthescript ALL = (root) NOPASSWD: /sbin/reboot ""
Note that if there's an entry for the same user in the sudoers file that requires a password, the NOPASSWD
entry must come after.
Once you trigger a reboot, you don't need to break the loop, the system will stop anyway.
If you decide to keep using this shell script, and your version of gpio
is too old to have the wfi
subcommand, here's an improved version which only checks the button state every second. Note that since the pin is only read once per second, this means you need to keep the button pressed for at least one second to be sure that the event is picked up.
gpio mode 0 in
while sleep 1; do
if [ "$(gpio read 0)" -eq 1 ]; then
reboot
fi
done &
What you have is known as a busy loop. Your loop will not consume hardly any memory, but it will consume plenty of CPU. This is typically mitigated by adding sleep
to the body of the loop.
while (true)
do
if [ `gpio read 0` -eq 1 ]
then
echo passowrd | sudo -S reboot
break
fi
sleep 1
done &
Getting rid of the busy loop will depend on what gpio
does. There are system calls such as select()
, which can block until a file descriptor is ready.
As far as efficiency, the ()
around the true
command actually executes true
in a subshell. This is not needed, and can be better expressed with the following:
while (( $(gpio read 0) != 1 )); do
sleep 1
done
echo passowrd | sudo -S reboot