Take the 2-minute tour ×

I've a Uni assignment I'm building that I need to run multiple times with multiple different parameters to check run times. At the moment I've a simple bash script that looks a little like the below - it logs what it's doing and the time results for each test, all normal output is discarded (I'm checking correctness elsewhere, don't worry).

The problem is that sometimes (and it's fairly rare) the master processes get stuck and never terminates, which obviously gets in the way of finishing the test run. I'm working on fixing that problem, obviously, but the algorithm is fine so I'd still like to press on with testing as there's a hand-in date approaching.

So, I'd like some way of specifying the maximum run-time of the processes, and if they exceed that I want them automatically terminated (preferably by SIGINT, or some other identified signal) and then the script to continue with the next entry. Or any suitable alternative that has the same sort of result.


Snippet of what I have at the moment:


echo "starting tests: clearing output file" > log

echo "running single tests" >> log

echo " starting 16" >> log
time (./single 16) 1> /dev/null 2>> log


echo "running multi tests" >> log

echo " starting 16 1 1 1" >> log
time (./master 16 4 7 100) 1> /dev/null 2>> log

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

"How do I run a command, and have it abort (timeout) after N seconds?", BASH FAQ entry #68.

share|improve this answer
Ha. The first line "FIRST check whether the command you're running can be told to timeout directly." was such a facepalm moment - I've written the program, so why don't I just stick a call to alarm() in there. Oh look, that solves it. Bit of a wood-for-the-trees moment, I think. Thanks, +1 and ticked. –  DMA57361 Nov 12 '10 at 20:26
add comment

Your Answer


