Take the 2-minute tour ×
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):

#!/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?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

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

timeout 1s myrunner $app
share|improve this answer
add comment

$! 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=$?
share|improve this answer
add comment

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

Your Answer

 
discard

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.