Linux 在同一个终端中一次并行运行多个命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10909685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run parallel multiple commands at once in the same terminal
提问by Oliver Zheng
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.
我想运行一些命令,每个命令在按下 Ctrl-C 之前不会退出。有什么我可以运行的东西一次运行所有这些,而 Ctrl-C 会退出它们吗?他们可以共享终端输出。
Specifically, I have the compass compiler, coffeescript compiler, and a custom command that watches for file changes all running watching for file changes. I don't want to load up a terminal for each command.
具体来说,我有指南针编译器、coffeescript 编译器和一个监视文件更改的自定义命令,所有这些命令都在监视文件更改。我不想为每个命令加载一个终端。
采纳答案by Alessandro Pezzato
This bash script is for N parallel threads. Each argument is a command.
这个 bash 脚本用于 N 个并行线程。每个参数都是一个命令。
trap
will kill all subprocesses when SIGINT is catched.wait $PID_LIST
is waiting each process to complete.
When all processes have completed, the program exits.
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";
Save this script as parallel_commands
and make it executable.
This is how to use this script:
将此脚本另存为parallel_commands
并使其可执行。
这是如何使用此脚本:
parallel_commands "cmd arg0 arg1 arg2" "other_cmd arg0 arg2 arg3"
Example:
例子:
parallel_commands "sleep 1" "sleep 2" "sleep 3" "sleep 4"
Start 4 parallel sleep and waits until "sleep 4" finishes.
启动 4 个并行睡眠并等待“睡眠 4”完成。
回答by Ole Tange
Use GNU Parallel:
使用 GNU 并行:
(echo command1; echo command2) | parallel
parallel ::: command1 command2
To kill:
杀死:
parallel ::: command1 command2 &
PID=$!
kill -TERM $PID
kill -TERM $PID
回答by Konstantine Rybnikov
I am suggesting a much simpler utility I just wrote. It's currently called par, but will be renamed soon to either parl or pll, haven't decided yet.
我建议使用我刚刚编写的更简单的实用程序。它目前称为 par,但很快将更名为 parl 或 pll,尚未决定。
API is as simple as:
API 非常简单:
par "script1.sh" "script2.sh" "script3.sh"
Prefixing commands can be done via:
前缀命令可以通过以下方式完成:
par "PARPREFIX=[script1] script1.sh" "script2.sh" "script3.sh"
回答by nsantana
Based on comment of @alessandro-pezzato.
Run multiples commands by using &
between the commands.
基于@alessandro-pezzato 的评论。通过&
在命令之间使用来运行多个命令。
Example:
例子:
$ sleep 3 & sleep 5 & sleep 2 &
It's will execute the commands in background.
它将在后台执行命令。
回答by Lord Hypersonic
To run multiple commands just add &&
between two commands like this: command1 && command2
要运行多个命令,只需&&
在两个命令之间添加,如下所示:command1 && command2
And if you want to run them in two different terminals then you do it like this:
如果你想在两个不同的终端中运行它们,那么你可以这样做:
gnome-terminal -e "command1" && gnome-terminal -e "command2"
This will open 2 terminals with command1
and command2
executing in them.
这将打开 2 个终端command1
并command2
在其中执行。
Hope this helps you.
希望这对你有帮助。
回答by hiroshi
It can be done with simple Makefile:
它可以通过简单的 Makefile 来完成:
sleep%:
sleep $(subst sleep,,$@)
@echo $@ done.
Use -j
option.
使用-j
选项。
$ make -j sleep3 sleep2 sleep1
sleep 3
sleep 2
sleep 1
sleep1 done.
sleep2 done.
sleep3 done.
Without -j
option it executes in serial.
如果没有-j
选项,它会串行执行。
$ make -j sleep3 sleep2 sleep1
sleep 3
sleep3 done.
sleep 2
sleep2 done.
sleep 1
sleep1 done.
You can also do dry run with `-n' option.
您也可以使用 `-n' 选项进行试运行。
$ make -j -n sleep3 sleep2 sleep1
sleep 3
sleep 2
sleep 1