how to detach process from the terminal in linux

In Linux and Unix systems, it is not uncommon to start a process from the command line or a terminal. In many cases, especially when logging remotely into a system without access to a graphical desktop, it is pretty much the only option.

One of the issues with starting the process from the terminal is that it starts as a child process of the shell itself. This means that when the terminal or shell is shut down or killed, it will also kill the running child processes. If you like a process that has been started from a terminal to remain running even after you have logged out and closed the terminal, then you need to detach the process from the terminal before closing the terminal.

Detaching or disassociating the process is where the child process is removed from the jobs table and disassociated from the parent process, such that closing of the parent shell will not kill the child process. There are a couple different options you have when detaching the child process.

disown command

If you are using the bash or zsh shell, then you can use the disown command to detach process. Using disown will remove the job from the system’s jobs table and make it such that it will not get SIGHUP or kill signals from the parent or the shell process. This makes it safe to log out of the shell later.

You have to execute the disown command from the same shell that the process is attached to. This means that if the process is active, then it need to be stopped first and pushed as a background job before you can disown it. Hit Ctrl-Z to stop the process and get the prompt. Then issue the command bg to restart the process in the background.

You can now disown the process by using either a process id, the job number in the current shell, or the job name.

bash$ disown <processid>

where processid is the id the process that want detached from a shell.

bash$ disown %number

where %number refers to the job number, for example it could be %2 or %3. The jobs are processes running in the background in the current shell. You can view the current jobs by using the jobs command.

bash$ disown %string

where %string refers to jobs that have names that starts with what is specified as string.

You can also execute this command without any command line arguments, in which case the most recent background job is used as the default. When the parent shell is killed, the process is not sent a SIGHUP signal and the init becomes the parent process.

nohup command

If you already know at the time of starting up the process that you want to run the process without attaching it to the shell, then you can use the nohup command at startup. The nohup command is a posix utility that configures a command to ignore the SIGHUP signal.

bash$ nohup firefox &

In the simplest form, the above example starts the firefox program in the background disassociated from the command shell. Although this works in most cases, when there is output being spitted out by the program, it could hang the terminal when attempting a logout. This could also be the case when you are remotely logged in using an ssh session.

bash$ nohup firefox > firefox-temp.out 2> firefox-temp.err < /dev/null &

In order to prevent the hanging of terminals or sessions, you can redirect all three of the I/O streams of the program as shown above. Some versions of the nohup, not all though, has a -p option that accepts a process id, that allows a running process to be nohup-ed.

screen command

Another option is to use the screen command, although it is a little more involved than the previous methods. Basically, screen can be used to detach the process and attach it back at a later time if needed. The screen command also gives you the ability to start multiple programs if needed.

bash$ screen firefox

This will start the program firefox. You can now detach the screen from the terminal. In order to detach, from the screen window press Ctrl-A followed by d to detach the screen from the terminal. You can also detach the screen from another terminal, if you have access to one.

bash$ screen -d <screenid>

You can find the screens and their ids by using the ls command line option.

bash$ screen -ls

You can reattach the screen later, if needed by using the -r command line option with the screen id.

bash$ screen -r <screenid>

All of the above methods will allow you to run a process or program detached from the shell it was started from. If you have used disown, it is very hard to reattach to the terminal later if needed.