Running Processes In Background – Terminal / BASH (Mac OS X / Raspberry Pi)

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:

import time
for x in range(10000000000):

Python Script to print a line lots

The & Method

The ampersand (&) method is achieved by placing a & at the end of the command. For example:

python3 &

This command is telling the terminal to open Python3 and run in the backround, which frees the terminal up for other commands, for example pinging

ampersand multitask
ampersand multitask

The [1] 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

ctrl z mulitask
ctrl z mulitask

With our script stopped (paused) we need to note the job number, [3] in this case and then enter bg %JOBNUMBER:

bg %3

Screenshot 2018-11-14 at 20.06.39.png

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

Keeping Track

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.

Lots of Background tasks

Using the command jobs -l produces a list of running jobs, their job number in [] and their process ID:

jobs l running jobs

For example, 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

suspending jobsIf 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 we would type kill -cont 10262