I want to run a few commands, each of which doesn't quit until Ctrl+C is pressed. Is there something I can run to run all of them at once, and Ctrl+C will quit them all? They can share the terminal output.
具体来说,我有指南针编译器、coffeescript 编译器和一个监视文件更改的自定义命令,所有这些命令都在运行监视文件更改。我不想为每个命令加载一个终端。
此 bash 脚本适用于 N 个并行线程。每个参数都是一个命令。
trap
当捕获到 SIGINT 时将杀死所有子进程。
wait $PID_LIST
正在等待每个进程完成。
当所有进程完成后,程序退出。
#!/bin/bash
for cmd in "$@"; do {
echo "Process \"$cmd\" started";
$cmd & pid=$!
PID_LIST+=" $pid";
} done
trap "kill $PID_LIST" SIGINT
echo "Parallel processes have started";
wait $PID_LIST
echo
echo "All processes have completed";
将此脚本另存为parallel_commands
并使其可执行。
这是使用该脚本的方法:
parallel_commands "cmd arg0 arg1 arg2" "other_cmd arg0 arg2 arg3"
Example:
parallel_commands "sleep 1" "sleep 2" "sleep 3" "sleep 4"
启动 4 个并行睡眠并等待“睡眠 4”完成。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)