1

I have the following bash script for running some apps in a folder for 1 second each (for testing):

#!/bin/bash
for app in ./*.app
do
    myrunner $app &
    sleep 1
    killall -9 myrunner
done

But I think using "killall -9" is not the real best way for doing it, is there a better one?

3 Answers 3

2

I think you want to use timeout, e.g.

timeout 1s myrunner $app
1

$! is the pid of the last backgrounded process, so:

do
    myrunner $app &
    pid=$!
    sleep 1
    kill -9 $pid
done

You don't have to assign it to an intermediate variable there as sleep is not backgrounded, ie., you could just use kill -9 $!.

Useful related variable is $? the exit status of the last command, which you can get WRT a backgrounded process this way:

somecommand & wait $!
status=$?
1

You are giving your test subjects 1s of wallclock time, not running time. That can make quite a difference.

Why run them only for a fixed time, and not build in some finish criteria? This way there is no real assurance that whatever was to be tested really was done (specially if longer running tests join the set later).

1
  • +1 thanks for the advise. In my specific case, there's no problem. Commented Jan 30, 2013 at 11:12

You must log in to answer this question.

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