惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
aimingoo的专栏
aimingoo的专栏
I
InfoQ
B
Blog
WordPress大学
WordPress大学
Jina AI
Jina AI
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
阮一峰的网络日志
阮一峰的网络日志
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
Google Developers Blog
C
Check Point Blog
月光博客
月光博客
L
LangChain Blog
GbyAI
GbyAI

博客园 - 吴丹阳-V

换源神器 chsrc:一键为你的开发环境切换到最快镜像 python 什么是偏函数 python 多进程日志打印方法 CDN 带宽控制的几种方法 obsidian-things3-today插件 凡人修仙传动漫截图 Things 工作流 创建一个chrome独立APP窗口 202405081331 mac 鼠须管 - rime 如何使用命令行执行同步操作 linux-command-substitution(命令替换) 系统调用的具体过程以及注意点【包含AIGC】 为什么 echo -n 后面会带着一个百分号 网络-最多有多少个a类、b类和c类网络号 unicode 与 utf-8 与 utf-16【包含AIGC】 《你的第一本哲学书》 我喜欢的音乐歌手标签 nginx 进程模型-整体架构 linux - trap 命令 丰田五问法 webp 格式图片压缩效果
使用 shell 脚本启动固定数量的进程
吴丹阳-V · 2023-12-12 · via 博客园 - 吴丹阳-V

经典用法

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

wait(1p) - Linux manual page