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

时间:2021-10-16 01:13:54

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 个解决方案

#1


10  

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 stdout and

将仅重定向stdout和

command > file 2>&1 &

will redirect both stdout and stderr for command to file while running command in the background. This page has a good introduction to redirection in Bash. You can view the command's output "live" by tailing the file:

在后台运行命令时,会将stdout和stderr重定向到命令文件。这个页面很好地介绍了Bash中的重定向。您可以通过拖尾文件来查看命令的输出“live”:

tail -f file

I would recommend running background jobs with nohup or screen as user2676075 mentioned to let your jobs keep running after you close your terminal session, e.g.

我建议您使用nohup或screen作为user2676075运行后台作业,以便在关闭终端会话后让您的作业继续运行,例如:

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

#2


3  

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

#3


0  

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.

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

#1


10  

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 stdout and

将仅重定向stdout和

command > file 2>&1 &

will redirect both stdout and stderr for command to file while running command in the background. This page has a good introduction to redirection in Bash. You can view the command's output "live" by tailing the file:

在后台运行命令时,会将stdout和stderr重定向到命令文件。这个页面很好地介绍了Bash中的重定向。您可以通过拖尾文件来查看命令的输出“live”:

tail -f file

I would recommend running background jobs with nohup or screen as user2676075 mentioned to let your jobs keep running after you close your terminal session, e.g.

我建议您使用nohup或screen作为user2676075运行后台作业,以便在关闭终端会话后让您的作业继续运行,例如:

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

#2


3  

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

#3


0  

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.

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