小编Tro*_*fin的帖子

使用后台进程捕获并退出大型 shell 脚本

我有 2 个包含 ffmpeg 命令的 shell 脚本(command1.sh 和 command2.sh)。command2.sh 有大约 500 个 ffmpeg 命令,这些命令通过“;”依次触发,而 command1.sh 处理音频 ffmpeg 命令。

主要问题:杀死整个脚本需要太多时间,并且它会占用我执行另一个脚本所需的 1-2 分钟的 CPU 功率,所以我白白失去了 CPU 功率,因为​​我无法立即杀死它。

代码: 我有init.sh其中包含:

trap 'print TERM received;exit' 15
chmod +x command1.sh;
chmod +x command2.sh;
./command1.sh & ./command2.sh
Run Code Online (Sandbox Code Playgroud)

所以它会在后台触发。

然后我执行pkill init.sh并捕获 shell陷阱退出shell 脚本,但我得到 exitCode: 1, failed: true ,并且后台的命令仍执行 1 分钟,直到它们被另一个kill ${pid 杀死我在 pkill 之后执行。

linux shell scripting

5
推荐指数
1
解决办法
1243
查看次数

标签 统计

linux ×1

scripting ×1

shell ×1