I am using jobs
command to control the number of compute-intensive processes. I want to not run more than max_cnt
processes at a time, and stop only when all the processes have stopped.
I use below bash script to accomplish this. However, this code always lists one process as running even after everything has executed and stopped.
Moreover, I can't find that process listed in htop
's list of processes. What should I do or where should I look for that process that is listed by the result of echo $(jobs -p)
command and how should I fix this issue of not exiting even when everything stops.
#!/usr/bash
SLEEP=5
max_cnt=8
# generate a random number less than eq $1
function random {
rand=$RANDOM
while [ "$rand" -gt $1 ]
do
rand=$RANDOM
done
}
function job {
# resource intensive job simulated by random sleeping
echo param1="$1",param2="$2"
random 20
echo Sleeping for $rand
sleep $rand
}
for param1 in 1e-6 1e-5 1e-4 1e-3 1e-2
do
for param2 in "ones" "random"
do
echo starting job with $param1 $param2
job $param1 $param2 &
while [ "$(jobs -p|wc -l)" -ge "$max_cnt" ]
do
echo "current running jobs.. $(jobs -p|wc -l) ... sleeping"
sleep $SLEEP
done
done
done
while [ "$(jobs -p|wc -l)" -ge 1 ]
do
echo "current running jobs.. $(jobs -p|wc -l) ... sleeping"
sleep $SLEEP
echo $(jobs -p)
done