Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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

for app in ./*.app
    myrunner $app &
    sleep 1
    killall -9 myrunner

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

share|improve this question

3 Answers 3

up vote 2 down vote accepted

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

timeout 1s myrunner $app
share|improve this answer

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

    myrunner $app &
    sleep 1
    kill -9 $pid

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 $!
share|improve this answer

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

share|improve this answer
+1 thanks for the advise. In my specific case, there's no problem. – Tom Brito Jan 30 '13 at 11:12

Your Answer


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.