Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


while true; do
  hasIt=$(cat ~/someFolder/hazText.txt) # 1: has text

  if [ $hasIt -eq "1" ]
    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
    sleep $checkTime

share|improve this question
Can 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 Jun 8 '13 at 14:45
Also, some distributions cause .bashrc to source .bash_profile that could explain why the script is called many times. – terdon Jun 8 '13 at 14:50
yes I can (also, I run Debian stable) – Gerreffo Jun 8 '13 at 14:51
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 Jun 8 '13 at 15:17
@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 Jun 9 '13 at 6:08

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

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .