2
I have made a simple .sh script which checks every minute whether a particular file is empty or not, and if it is not empty, makes a led on my laptop blink at 0.5Hz.
I call this script from my .bash_profile
.
Whenever I am in TTY, the thing works flawlessly (to say: when the file is not empty the led on my lappy blinks at that precise frequency).
But when I run X (I use Xmonad/urxvt, invoked via startx
if that could help) something strange happens. The frequency of the blink is erratic, it slows down (a bit) the machine and when I call top
I see various sudo processes (the one I call to turn the led on or off).
It is the same behavior as if I called the script three or four times.
I don't know how to diagnose the problem. The manual says .bash_profile
gets read once (at login time). Can you help me out?
# blink mail led
blinkTime="0.2"
checkTime="60"
while true; do
hasIt=$(cat ~/someFolder/hazText.txt) # 1: has text
if [ $hasIt -eq "1" ]
then
echo "1" | sudo tee /sys/class/leds/led:alarm/brightness > /dev/null
sleep $blinkTime
echo "0" | sudo tee /sys/class/leds/led:alarm/brightness > /dev/null
sleep $blinkTime
else
sleep $checkTime
fi
done
1Can you share the script? I guess you have some kind of sleep logic in there to slow down the looping of the script. Bear in mind that
PATH
variables may not be available if you run scripts this way. So if you are callingsleep
for example you actually have to use the absolute path (can be determined viawhich sleep
) otherwise your system won't be able to execute it. – leepfrog – 2013-06-08T14:45:30.047Also, some distributions cause
.bashrc
to source.bash_profile
that could explain why the script is called many times. – terdon – 2013-06-08T14:50:02.953yes I can http://paste.debian.net/9196/ (also, I run Debian stable)
– Gerreffo – 2013-06-08T14:51:24.760Don't know why your script gets called multiple times but you can fix that by using a lockfile. This answer on SO shows you one way to do it.
– Nifle – 2013-06-08T15:17:27.480I don't understand your echo lines. Doesn't
sudo echo "1" | /sys/class/leds/led:alarm/brightness > /dev/null
work? – Nifle – 2013-06-08T15:23:12.143@Nifle no, that uses a pipe to pass a string through a file, he uses
tee
to actually write to the file. – terdon – 2013-06-08T15:34:15.067Is your script running multiple times? Check with
ps aux | grep SCRIPTNAME
– terdon – 2013-06-08T15:34:51.337echo "0" | sudo tee /sys/class/leds/led:alarm/brightness > /dev/null Why not: sudo echo "0" > /sys/class/leds/led:alarm/brightness ? Add script execution to /etc/rc.local and remove from .bash* to avoid duplicates, or use lock file. – september – 2013-06-08T17:12:45.927
1
@september:
– Gordon Davisson – 2013-06-09T06:08:58.333| sudo tee
is needed because a normal redirect would be done (or rather, would fail) in the calling shell, beforesudo
even launches. See this previous question@Gordon, @september: Although
sudo sh –c "echo '0' > /sys/class/leds/led:alarm/brightness"
would probably work. But usingtee
is a clever trick. – Scott – 2013-06-11T01:17:42.230