×

Search anything:

Run a process in background on Linux

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

Reading time: 20 minutes | Coding time: 10 minutes

Running a process in the background means that the process will not occupy the screen and we can continue working on it with different commands. The process will continue running but output can come to the screen but it will not take any input. We can later bring it back on the screen as well.

In short, the syntax is:

<command> &

Note that this does not mean that the process will continue to run on closing the terminal. If you want to the process to run, we on closing the terminal following the different techniques given here.

Note, there are two types of processes namely:

  • background process
  • foreground process

The idea is that the background process does not link with the STDOUT and STDERR pipes of the screen session while foreground process is linked with the STDOUT and STDERR pipes of the screen session. Hence, if the background process produces an output in the terminal, it will get printed. So, it is useful if an process does not produce any output or output is piped to a file.

Note that both background and foreground processes are linked with STDIN pipe. So, if a background process needs an input from STDIN pipe, it gets stuck (hang) until we bring it to the foreground.

To make a process run in the background, we need to append the command with &.

Syntax:

command &

Example:

python code.py &

It will give an output as:

[1] 121378

1 is the job id and 121378 is the process id.

Check background processes

We can check the processes that are in background as follows:

jobs

The output in our case is as follows:

[1]-  Running                 python code.py &
[2]+  Running                 python opengenus.py &

The first integer is the Job ID of the command that is given along side.

For example, the command "python opengenus.py &" has Job ID 2.

We can use the Job ID to manage the background process and do several things like:

  • bring a background process to foreground
  • killing a background process

Bring a process back

To bring a process back in foreground, we need to use the following command:

fg %<job_ib>

Example:

fg %2

This will bring the process "python opengenus.py &" to the foreground. The output will be like:

(base) [opengenus@localhost Desktop]$ fg %2
python opengenus.py

If your code is waiting for an input, it can be provided now.

Make a running process go into background

Now, if your process is already running and you want to take it to the background, you can follow the following steps:

Press control key + Z

// check job id using jobs
jobs

// make job go to background
bg %<job id>

The process will have moved to the background.

If you just press control key + z. It will stop the process which we can see in the jobs command:

[1]   Running                 python code.py &
[2]-  Running                 python opengenus.py &
[3]+  Stopped                 python code_2.py

Now, if we move the Job ID 3 to background using bg command, we can check the impact using the jobs command.

bg %3

It will give an output as:

[3]+ python code_2.py &

Note the & sign at the end. It signifies that the process is running in the background.

Try the jobs command:

jobs

The output will be as follows:

[1]   Running                 python code.py &
[2]-  Running                 python opengenus.py &
[3]+  Running                 python code_2.py &

Get Process ID of a job

We can get the process ID of all background jobs using the jobs command as follows:

jobs -l

The output will be as follows:

[1]  121378 Running                 python code.py &
[2]- 127536 Running                 python opengenus.py &
[3]+ 12623 Running                 python code_2.py &

Note that the first number (1, 2 and 3) are Job IDs while the second numbers(like 121378) are Process ID.

Kill a process

There are two ways to kill a process:

  • Kill it directly using process ID
  • Bring it to foreground and kill it using control + C

Once we get the process ID of a job, we can kill it directly like:

kill 121378

This will kill the first job. It will give an output as:

[1]   Terminated              python code.py

We can verify this using our jobs command as follows:

jobs -l

The output will be as:

[2]- 127536 Running                 python opengenus.py &
[3]+ 12623 Running                 python code_2.py &

Notice that the first background job is missing.

We will use the second approach of bring it to the foreground and killing it as follows.

fg %3

This is give an output as:

(base) [opengenus@localhost Desktop]$ fg %3
python code_2.py

The terminal is stuck. If we press Control + C, the process will be terminated as follows:

(base) [opengenus@localhost Desktop]$ fg %3
python code_2.py
^CTraceback (most recent call last):
  File "code.py", line 7, in <module>
    time.sleep(1)
KeyboardInterrupt

We can verify this using jobs -l command.

jobs -l

The output will be as follows:

[2]+ 127536 Running                 python opengenus.py &

Hence, only process is in the background.

With this, you have the complete knowledge of working with background process.

The problem with this is that if you close the terminal it will kill the process. To enable the process to continue even after closing the terminal, we need to detach the process from the terminal which can be done using screen, disown and nohup.

OpenGenus Tech Review Team

OpenGenus Tech Review Team

The official account of OpenGenus's Technical Review Team. This team review all technical articles and incorporates peer feedback. The team consist of experts in the leading domains of Computing.

Read More

Improved & Reviewed by:


Run a process in background on Linux
Share this