Bash script execute multiple programs as separate processes

As the title says, how to write a bash script that will execute, for example, three different Python programs as separate processes? And then I can access each of these processes to see what is being registered on the terminal?

Edit: Thanks again. I forgot to mention that I know about adding & , but I'm not sure how to access what is displayed on 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 was displayed.

+4
source share
3 answers

You can run the task in the background as follows:

 command & 

This allows you to run several tasks in a row, without waiting for the end of the previous one.

If you run several background jobs like this, they will all have the same stdout (and stderr ), which means that their output will probably alternate. For example, take the following script:

 #!/bin/bash # countup.sh for i in `seq 3`; do echo $i sleep 1 done 

Run 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 it may also look like this:

 1 2 1 3 2 3 

You probably do not want this, because it would be very difficult to figure out which solution belongs to the task. Decision? Redirecting stdout (and optionally stderr ) for each job to a separate file. for instance

 command > file & 

will only redirect stdout and

 command > file 2>&1 & 

will redirect both stdout and stderr for command to file when running command in the background. This page provides a good introduction to redirection in Bash. You can view the output of the "live" command with the tail file:

 tail -f file 

I would recommend running background jobs using nohup or screen , as indicated by user 2676075, so that your jobs continue to work after closing a terminal session, for example

 nohup command1 > file1 2>&1 & nohup command2 > file2 2>&1 & nohup command3 > file3 2>&1 & 
+10
source

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 call files when running commands. Remember that you can link them all by being in the directory and doing "tail * .log"

Alternatively, you can configure the script to create 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 using the screen list and screen -r [screen name]

Enjoy

+3
source

Another option is to use a terminal emulator to start three processes. You can use xterm (or rxvt, etc.) if you use X.

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

Depends on what you want. This approach allows you to display terminal windows so you can see the result in real time. You can also combine it with redirection to save the result.

0
source

Source: https://habr.com/ru/post/1496681/


All Articles