How to detach a Linux process from shell

When you execute a Linux command resulting in a background process, the process will be killed automatically if a controlling terminal is closed, or a ssh session is terminated. It is because a shell, which is the parent process of all background processes running in the terminal/ssh session, sends a SIGINT to all its child processes. If you want a process to keep running after a terminal session is over, that is when detaching a Linux process from shell can help.

Here is how you can detach a process from bash shell.

If a given process is running in the foreground, press Ctrl+z to interrupt it.

Then run it in the background.

$ bg
[1]+ my_command &

Finally, type disown along with job sequence number of the backgrounded job.

$ disown %1

disown is a built-in shell command that causes a shell to not send SIGHUP to disowned child processes, thereby allowing them to continue even after the shell process is terminated. Once a background process is disowned by a shell, the process will disappear from a job list when you type jobs command, which means that you can safely close a terminal or log out without killing the process.

When run without any argument, disown removes the most recent job from a job list.

Not all shells support disown, unfortunately. disown is supported in bash and zsh, but shells like csh, tcsh, dash do not support it.

Besides disown, you can also use nohup to detach a Linux process from shell, but in this case, at the time of process launch.

$ nohup <my_command> &

While disown prevents a closing shell from sending SIGUP signals, nohup configures a command to ignore SIGHUP signals sent by the shell. So once any command is launched with nohup, it does not matter whether or not the shell sends out SIGHUP. Any nohup-ed command will continue to run.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

One thought on “How to detach a Linux process from shell

Leave a comment

Your email address will not be published. Required fields are marked *

Current ye@r *