In the past I have launched multiple Terminal / BASH sessions when I have wanted to run multiple commands, however it transpires that there is an easier method. Using the background / foreground options in Terminal / BASH allows one session to run multiple processes at the same time.
Throughout this post I will be using a Python script that simply does:
The & Method
The ampersand (&) method is achieved by placing a & at the end of the command. For example:
python3 test.py &
This command is telling the terminal to open Python3 and run test.py in the backround, which frees the terminal up for other commands, for example pinging http://www.google.com.
The  in the above is the commands job number and the 10088 is the commands process identification, more on this later.
The & method continues to output the Python script but allows for new commands to be entered.
The BG % / FG % Method
The BG % / FG % (Background / Foreground) method is slightly different as it allows us to move a running command to the background after it has started running. First we need to start the Python script running:
And once the script is running we need to pause it using CTRL Z
With our script stopped (paused) we need to note the job number,  in this case and then enter bg %JOBNUMBER:
And our job then resumes running in the background. To bring the job back to foreground we need to enter fg %JOBNUMBER, so in our case fg %3
With the possibility to start multiple jobs and have them all running in the background it could be very easy to lose track of what is running. However, there is a solution for that and that solution is jobs.
jobs has the ability to list running or suspended jobs, to stop a job or to continue a job.
For this example I have copied my Python script four times, modifying the output depending on the script number and then set them all running in the background.
Using the command jobs -l produces a list of running jobs, their job number in  and their process ID:
For example, test.py is running as process ID 10262. With this knowledge we can then stop (pause/suspend) a job using the command kill -stop PROCESSID. So kill -stop 10262 suspends test.py
If we suspend all the running jobs and then type jobs -l it will show all the jobs as suspended. To start a job running again (unpausing it) we need to type kill -cont PROCESSID . So to start test.py we would type kill -cont 10262
You must be logged in to post a comment.