使用 shell 脚本启动固定数量的进程
经典用法
i=0
while true;do
# 此文件里面可以放同时执行的数量,也可将其替换成一个固定值
mod=$(cat process_count.plain.txt)
# 并行执行数量, 默认为3个进程执行
if [ -z "$mod" ]; then
mod=3
fi
if [ $((i % mod)) -eq "0" ]; then
echo "running $i"
# 关键在这里,需要等待上一批脚本执行完成
wait
fi
nohup sh your_script.sh >> your_script_${i}.log 2>&1 &
((i++))
done
wait 的作用
Linux 中的 wait 命令用于等待子进程的状态变化,并获取有关状态变化的子进程的信息。状态变化包括:
- 子进程终止
- 子进程被信号停止
- 子进程被信号恢复
wait [pid]
当子进程一直在执行的时候,wait会阻塞
如果接收到指定的信号后,会唤醒并向下执行
for pid in $(jobs -p); do
wait $pid
status=$?
if [ $status != 0 ]; then
echo " $pid status is $status have some error!" >> your_log
else
echo "$pid status is $status success!" >> your_log
fi
done
本文来自博客园,作者:吴丹阳-V,转载请注明原文链接:https://www.cnblogs.com/wudanyang/p/17895896.html