Running processes in the background in Linux systems represents one of the most common ways to perform some batch process or long running process in parallel, without keeping busy your current terminal and consequently forcing you to wait until the process terminates or to create a new additional session to continuing working on the system.
On the other hand, since it is not blocking your terminal or the process is not tied to your session when running in the background, you are free to close the session without terminating the process you are currently executing.
Moreover, by running process in the background, it is not needed to configure it as a service previously to achieve the same effect, so it could be more convenient for executing scripts that are expected to consume more time in a on demand basis.
Nevertheless, if the same script or process needs to be started when system starts, it is recommended to configure it as a service, then configure it to run when system starts up.
Here, in this post, I will explain some use cases to show the different ways to run processes in the background on Linux, how to stop or terminate them when they are in this state and how to bring them back to foreground.
The script used for the examples is the following and it is named “hello_world.sh”:
#!/bin/bash
i=1
while true
do
echo "$i hello World"
sleep 3
((i++))
done
The above script will print every 3 seconds the counter “i” followed by the string “hello world” indefinitely.
Execute script or process in background with ampersand
This is the easiest way to run a process or script in the background directly, without even blocking your current session and terminal. In order to use this method, you simply need to add the ampersand (&) at the end of the command to run the process in the background:
$ ./hello_world.sh &
As you may notice, the output of the script is printing on the terminal and visually interferes with other tasks you want to continue doing. Nevertheless, running processes in background doesn’t impedes you to redirect the output to a file in order to avoid the interference:
$ ./hello_world.sh &>out.txt &
The above command will send both output (stdout) and errors (stderr) from the script to “out.txt” file so it will not longer annoy your current terminal.
Run a script or process and move it to the background with bg
Another situation that you may encounter is when you running a process and script that is taking too long to end, and you want to close the terminal session without terminating the process and starting over again.
For this case, what you can do is to issue a CTRL+Z in order to stop the process and then run bg
for continuing executing the process in the background:
You can check the stopped processes with the command jobs
:
$ jobs
The above output from jobs
command it shows a number between brackets which represents the job number and the plus “+” symbol indicates that this is the default job. If you want to resume the default process (that one marked with “+”), then by running the following command without any options it will do it:
$ bg
But, if you want to run a particular job from the list, then you need to run bg %<job number>
:
$ bg %1
Using nohup to run script or process in the background
The nohup
command it helps you to run processes in the background but keeping your terminal busy. The only difference is, if you decide to close the terminal or the connection to the terminal ends unexpectedly, the process will still running on the system.
The nohup
command by default prints stdout and stderr to a file called “nohup.out” in the same path where the process is, but it can be changed. It is also possible to combine it with the ampersand to avoid blocking the terminal with the process that nohup
is running. Nevertheless, the behaviour it will be similar to run any process with ampersand and redirecting any output or errors to a file.
To use nohup
, execute it followed by the process you want to run in these terms:
$ nohup ./hello_world.sh
Then, to test what is the effect of running a process with nohup
, in the picture below you may appreciate 2 different sessions. The above one, shows that the current bash process has the PID 26157 and afterwards, runs the script “hello_world.sh” with nohup.
Then, in the below terminal, you can see the “hello_world.sh” script running so, to test the background processing of nohup
, in the below terminal a kill -9
to the above bash PID session is issued in order to simulate the terminal closing or session disconnection. However, thanks to the nohup the process is still running as you can see on the picture.
Bring a process back to foreground with fg
If it is the case that you want to bring back a process from the background or resume the execution in the foreground from the stopped status, then you might want to use the command fg %<job number>
:
$ fg %1
In the below picture, you can see how the script “hello_world.sh” is managed with commands fg, bg, jobs
and the CTRL+Z:
In the first command line, the process is running in the foreground, blocking the terminal. Then CTRL+Z is issued move the process in stopped mode. Next, a fg
is executed in order to bring it back to the same status as previously then, again a CTRL+Z sends the process to stopped status again. afterwards, a bg
sends the process to run in background and, subsequently, a fg
is executed to move the background process to foreground directly.