Force a bash script to run to completion where some items may stick
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.
Suggestions?
Snippet of what I have at the moment:
#!/bin/bash
echo "starting tests: clearing output file" > log
echo "running single tests" >> log
echo " starting 16" >> log
time (./single 16) 1> /dev/null 2>> log
time (./single 16) 1> /dev/null 2>> log
time (./single 16) 1> /dev/null 2>> log
#etc
echo "running multi tests" >> log
echo " starting 16 1 1 1" >> log
time (./master 16 4 7 100) 1> /dev/null 2>> log
time (./master 16 4 7 100) 1> /dev/null 2>> log
time (./master 16 4 7 100) 1> /dev/null 2>> log
#etc
"How do I run a command, and have it abort (timeout) after N seconds?", BASH FAQ entry #68.