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?
I guess the $2 & is where you run the long function right? I had the same problem and some time, depending on what is in the function you will have multiple process ... I don t know if my solution is the best way to do it, but it worked for me.
change :
$2 &
for :
awk '{system($2)}' &
this way, pid =$! will give you the awk pid and by killing the awk, you kill the whole process thing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.