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

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

Timeout sounds like what you are looking for.

man timeout
timeout - run a command with a time limit
share|improve this answer

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

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

You must log in to answer this question.

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