我编写了一个脚本,使所有子进程从某个特定进程开始,并在nodefile.dat中给定的一组节点上使用给定的字符串“ mystring.sh”启动。该脚本可以运行,但是经过几次迭代后速度却变得很慢。任何解释或建议,如何改进它。我正在使用脚本检查HPC上的计算。
!/bin/bash
PID=$1;
Iter=1;
ChildPIDs=""
function getcpid() {
cpids=($(ssh $2 "pgrep -P $1|xargs"))
#echo "cpids=$cpids"
#sleep 1
Iter=(${Iter[@]} ${Iter[@]});
ChildPIDs=(${ChildPIDs[@]} ${cpids[@]})
for cpid in $cpids;
do
#echo "$cpid"
Iter=$(($Iter+1))
#echo $Iter
#if [ $Iter -ge 20 ]; then break; fi
getcpid $cpid $2
done
}
for Server in $(cat nodefile.dat); do
for PID in $(ssh $Server "ps -fu user | grep 'mystring.sh' | awk {'print \$2'}"); do
getcpid $PID $Server
if [ -z "$ChildPIDs" ]; then
echo $PID starts no process on $Server
else
echo $PID starts processes ${ChildPIDs[@]} on $Server
fi
unset ChildPIDs
done
done