1

I want to create a loop (blinking LED) while a command (in this case ping) is running. I am using the Raspberry Pi (Raspbian)

while [ `nmap -p 80 example.com` ] # something like this
    do 
    echo "1">/sys/class/gpio/...
    sleep 0.2
    echo "0">/sys/class/gpio/...
    sleep 0.2
done
1
  • Perhaps you could run ping as a child process, and have the parent process do the light-blinking whilst it waits for the child to finish what it's doing. Commented Nov 10, 2014 at 16:54

1 Answer 1

7

What I would do :

any_command & _pid=$!
while kill &>/dev/null -0 $_pid; do
    echo "1">/sys/class/gpio/...
    sleep 0.2
    echo "0">/sys/class/gpio/...
    sleep 0.2
done
  • kill -0 just test if the pid exists =)
  • the command any_command is launched in the background
  • & put the command in the background
  • $! is the integer of the pid of the latest backgrounded job
Sign up to request clarification or add additional context in comments.

4 Comments

+1, nice. But why do you put the "&>/dev/null" in such a weird place?
And maybe change the test by some use of jobs | grep any_command as the pid could be reused
@OlivierDulac I like to place &>/dev/null here, that demonstrate this possibility. And your command will have a bigger footprint than mine.
Yes, no subshell needed, indeed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.