In this guide, we shall bring to light a simple yet important concept in process handling in a Linux system, which is how to completely detach a process from its controlling terminal.
When a Linux process is associated with a terminal, two problems might occur:
- Your controlling terminal is filled with so much output data and error/diagnostic messages.
- In the event that the terminal is closed, the process together with its child processes will be terminated.
To deal with these two issues, you need to totally detach a process from a controlling terminal. Before we actually move to solve the problem, let us briefly cover how to run processes in the background in Linux.
Run Linux Command or Process in Background
If a process is already in execution, such as the tar command example below, simply press
Ctrl+Z to stop it then enter the command
bg to continue with its execution in the background as a job.
You can view all your background jobs by typing
jobs. However, its stdin, stdout, and stderr are still joined to the terminal.
$ tar -czf home.tar.gz . $ bg $ jobs
You can as well run a Linux process in the background using the ampersand,
$ tar -czf home.tar.gz . & $ jobs
Take a look at the example below, although the tar command was started as a background job, an error message was still sent to the terminal meaning the process is still connected to the controlling terminal.
$ tar -czf home.tar.gz . & $ jobs tar: ./.config/etcher: Cannot open: Permission denied
Keep Linux Process Running After Logout
We will use the disown command, which is used after the process has been executed and put in the background, its work is to remove a shell job from the shell’s active list jobs, therefore you will not use
bg commands on that particular job anymore.
In addition, when you close the controlling terminal or log out, the job will not hang or send a SIGHUP to any child jobs.
Let’s take a look at the below example of using the diswon bash built-in function.
$ sudo rsync Templates/* /var/www/html/files/ & $ jobs $ disown -h %1 $ jobs
You can also use the
nohup command, which also enables a process to continue running in the background when a user exits a shell.
$ nohup tar -czf iso.tar.gz Templates/* & $ jobs
Detach a Linux Process From Terminal
Therefore, to completely detach a process from a controlling terminal, use the command format below, this is more effective for graphical user interface (GUI) applications such as Firefox:
$ firefox </dev/null &>/dev/null &
In Linux, /dev/null is a special device file that writes off (gets rid of) all data written to it, in the command above, input is read from, and output is sent to /dev/null.
As a concluding remark, provided a process is connected to a controlling terminal, as a user, you will see several output lines of the process data as well as error messages on your terminal. Again, when you close the controlling terminal, your process and child processes will be terminated.
Importantly, for any questions or remarks on the subject, reach us by using the comment form below.