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!

math
  • 2,693

4 Answers4

4

Timeout sounds like what you are looking for.

man timeout
timeout - run a command with a time limit
Ole Tange
  • 5,099
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...

hotei
  • 3,693
  • 2
  • 21
  • 24
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.

Rich Homolka
  • 32,350
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.

math
  • 2,693