Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

for debugging reasons I want to spot one of those bugs showing up just occasionally. Therefore I want to code a while loop in the shell

  • starting the program (if it will segfault => that's what I want)
  • kill the program after a certain timeout (e.g. some seconds)

Problem here is that the PID will change. Can you give me a hint on how to perform this? I use the zsh, but other shells are welcome too!

share|improve this question
add comment

4 Answers

Timeout sounds like what you are looking for.

man timeout
timeout - run a command with a time limit
share|improve this answer
add comment
up vote 0 down vote accepted
#/usr/bin/env zsh
for i in `seq 1 10`; do
    echo "start sleep";
    sleep 30&
    LAST_CHILD=$!
    echo "PID\$!: "$LAST_CHILD;
    echo "wait for kill:"
    sleep 1 && kill -9 $LAST_CHILD;
done

should work with bash as well, but I still need some segfault detection so the loop aborts.

share|improve this answer
    
There's no need to use seq. –  Dennis Williamson Aug 13 '10 at 20:23
    
@Dennis: I know this is just an example, in practice I would use a while((true)) loop. –  math Aug 14 '10 at 7:05
1  
No, I meant for i in {1..10}. –  Dennis Williamson Aug 14 '10 at 13:55
add comment

Try the bash script here:

http://www.cyberciti.biz/faq/shell-scripting-run-command-under-alarmclock/

It does a spawn of a watchdog process that knows the PID of the subcommand to run. Caveat: in this script, the command to kill needs to be the immediate child of the script.

share|improve this answer
add comment

Haven't done this for a while but the basic idea is to get the running shell to send it's PID to a named file before it starts looping and then read that file back in with a separate killer program that has the timer on it. It's not "hard", but the devil's in the details...

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.