Suppose I have bash-script with following code:
function test() {
some_code
...
make
some_code
}
test
some_other_code
test() could contain any code that might run unreasonably long.
I was trying to use something like:
function test() {
cd $WORK_FOLDER
make
}
run_timeout()
{
local timeout=$1
$2 &
local pid=$!
while ps $pid >/dev/null && [ $timeout -ne 0 ]; do
sleep 1
let timeout--
done
kill -9 $pid 2>/dev/null && echo "Process $pid killed because executed too long"
}
run_timeout 15 "test"
run_timeout 5 "test"
But make was still running after the estimated time.
Any suggestion how to solve this problem?
Is there any technique that prevents a bash script from hanging?