让 bash 脚本作为单独的进程执行多个程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18197395/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:13:50  来源:igfitidea点击:

Have bash script execute multiple programs as separate processes

pythonlinuxbashscripting

提问by Petesta

As the title suggests how do I write a bash script that will execute for example 3 different Python programs as separate processes? And then am I able to gain access to each of these processes to see what is being logged onto the terminal?

正如标题所暗示的那样,我如何编写一个 bash 脚本,该脚本将作为单独的进程执行例如 3 个不同的 Python 程序?然后我是否能够访问这些进程中的每一个以查看登录到终端的内容?

Edit: Thanks again. I forgot to mention that I'm aware of appending &but I'm not sure how to access what is being outputted to the terminal for each process. For example I could run all 3 of these programs separately on different tabs and be able to see what is being outputted.

编辑:再次感谢。我忘了提到我知道附加,&但我不确定如何访问每个进程输出到终端的内容。例如,我可以在不同的选项卡上分别运行所有 3 个程序,并能够查看输出的内容。

回答by ThisSuitIsBlackNot

You can run a job in the background like this:

您可以像这样在后台运行作业:

command &

This allows you to start multiple jobs in a row without having to wait for the previous one to finish.

这使您可以连续启动多个作业,而无需等待前一个作业完成。

If you start multiple background jobs like this, they will all share the same stdout(and stderr), which means their output is likely to get interleaved. For example, take the following script:

如果您像这样启动多个后台作业,它们将共享相同的stdout(和stderr),这意味着它们的输出可能会交错。例如,采用以下脚本:

#!/bin/bash
# countup.sh

for i in `seq 3`; do
    echo $i
    sleep 1
done

Start it twice in the background:

在后台启动它两次:

./countup.sh &
./countup.sh &

And what you see in your terminal will look something like this:

您在终端中看到的内容将如下所示:

1
1
2
2
3
3

But could also look like this:

但也可能是这样的:

1
2
1
3
2
3

You probably don't want this, because it would be very hard to figure out which output belonged to which job. The solution? Redirect stdout(and optionally stderr) for each job to a separate file. For example

您可能不希望这样,因为很难确定哪个输出属于哪个作业。解决方案?将每个作业的stdout(和可选的stderr)重定向到一个单独的文件。例如

command > file &

will redirect only stdoutand

只会重定向stdout

command > file 2>&1 &

will redirect both stdoutand stderrfor commandto filewhile running commandin the background. This pagehas a good introduction to redirection in Bash. You can view the command's output "live" by tailing the file:

将重定向既stdoutstderrcommandfile而运行command在后台。这个页面很好地介绍了 Bash 中的重定向。您可以通过tailing 文件查看命令的输出“实时” :

tail -f file

I would recommend running background jobs with nohupor screenas user2676075 mentioned to let your jobs keep running after you close your terminal session, e.g.

我建议像 user2676075 提到的那样使用nohupscreen运行后台作业,让您的作业在关闭终端会话后继续运行,例如

nohup command1 > file1 2>&1 &
nohup command2 > file2 2>&1 &
nohup command3 > file3 2>&1 &

回答by Alex Atkinson

Try something like:

尝试类似:

command1 2>&1 | tee commandlogs/command1.log ;
command2 2>&1 | tee commandlogs/command2.log ; 
command3 2>&1 | tee commandlogs/command3.log
...

Then you can tail the files as the commands run. Remember, you can tail them all by being in the directory and doing a "tail *.log"

然后您可以在命令运行时拖尾文件。请记住,您可以通过进入目录并执行“tail *.log”来尾随它们

Alternatively, you can setup a script to generate a screen for each command with:

或者,您可以设置一个脚本来为每个命令生成一个屏幕:

screen -S CMD1 -d -m command1 ;
screen -S CMD2 -d -m command2 ;
screen -S CMD3 -d -m command3
...

Then reconnect to them later with screen --list and screen -r [screen name]

然后稍后使用 screen --list 和 screen -r [screen name] 重新连接到它们

Enjoy

享受

回答by Ziffusion

Another option is to use a terminal emulator to run the three processes. You could use xterm (or rxvt etc.) if you are using X.

另一种选择是使用终端模拟器来运行这三个进程。如果您使用 X,则可以使用 xterm(或 rxvt 等)。

xterm -e <program1> [arg] ... &
xterm -e <program2> [arg] ... &
xterm -e <program3> [arg] ... &

Depends on what you want. This approach lets you pop up the terminal windows, so you can see the output in real time. You can also combine it with redirection to save the output as well.

取决于你想要什么。这种方法可以让您弹出终端窗口,以便您可以实时查看输出。您还可以将其与重定向结合使用以保存输出。