Script acting strange in bash_profile (but just under X)

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

Gerreffo

Posted 2013-06-08T14:42:32.453

Reputation: 21

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 calling sleep for example you actually have to use the absolute path (can be determined via which sleep) otherwise your system won't be able to execute it. – leepfrog – 2013-06-08T14:45:30.047

Also, some distributions cause .bashrc to source .bash_profile that could explain why the script is called many times. – terdon – 2013-06-08T14:50:02.953

yes I can http://paste.debian.net/9196/ (also, I run Debian stable)

– Gerreffo – 2013-06-08T14:51:24.760

Don'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.480

I 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.067

Is your script running multiple times? Check with ps aux | grep SCRIPTNAME – terdon – 2013-06-08T15:34:51.337

echo "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: | sudo tee is needed because a normal redirect would be done (or rather, would fail) in the calling shell, before sudo even launches. See this previous question

– Gordon Davisson – 2013-06-09T06:08:58.333

@Gordon, @september: Although sudo sh –c "echo '0' > /sys/class/leds/led:alarm/brightness" would probably work. But using tee is a clever trick. – Scott – 2013-06-11T01:17:42.230

Answers

1

.bash_profile is executed when Bash starts as a login shell, not "at login time" as you wrote. This means that every time Bash starts either with "--login" option or as "-bash", it will read and execute .bash_profile. Most likely starting your X session implicitly starts several Bash instances (since it's a default shell on Linux). Some of these instances probably happen to be login shells, your script gets called severals times, which makes your LED go into disco mode.

The solution to your problem would be to move the script out of the .bash_profile, and either create a cron job for it, or rewrite it using inotify-tools.

KMZ

Posted 2013-06-08T14:42:32.453

Reputation: 166