3

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!

4 Answers 4

4

Timeout sounds like what you are looking for.

man timeout
timeout - run a command with a time limit
1

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

1

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.

0
#/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.

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

You must log in to answer this question.

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