I'm looking for a more efficient method of monitoring a file from my daemon. I wrote a script which loops to watch a couple of files (/proc/btn_sw1
and /proc/btn_sw2
)... I knew looping was a bad idea, I didn't realize how bad it would be.
My daemon is automatically started via the init process, and after it launched I checked the top
output and my process was #1 running ~17% CPU constantly:
PID PPID USER STAT VSZ %VSZ %CPU COMMAND
1698 1 root S 2196 0% 17% {resetd.sh} /bin/sh /etc/init.d/resetd
My daemon monitors the /proc entries (the value of them is just 1 or 0) which are set by the keypad driver when a hardware button is pressed/released. So I need to know when these file change in value.
Is there a way that I can have my daemon be woken up when the value of the file is changed? Note: I don't want to just sleep for X seconds in between each read because I need to time out how long the button has been pressed for and I don't want to miss the start.
My current daemon code:
#!/bin/sh
proc1file=/proc/btn_sw1
proc2file=/proc/btn_sw2
BTN1VAL=$(cat $proc1file)
BTN2VAL=$(cat $proc2file)
tic=0
elap_time=0
reset_met=0
until [ $reset_met -gt 0 ]
do
BTN1VAL=$(cat $proc1file)
BTN2VAL=$(cat $proc2file)
if [ $BTN1VAL -gt 0 ] && [ $BTN2VAL -gt 0 ]
then
tic=`date +%S`
# Start the 10second loop, I'm ok with reading in here, but before this I'd like
# to be sleeping or idle instead of constantly polling
until [ $elap_time -ge 5 ] || [ $BTN1VAL -lt 1 ] || [ $BTN2VAL -lt 1 ]
do
BTN1VAL=$(cat $proc1file)
BTN2VAL=$(cat $proc2file)
toc=`date +%S`
elap_time=`expr $toc - $tic`
done
if [ $elap_time -ge 5 ]
then
reset_met=1
else
elap_time=0
fi
fi
done
echo "Rebooting!"
reboot -f