Detach process from parent on Linux

Every program you run from your working shell is, to the Linux system, regarded as a process. Each process, except init, has its parent identified by PPID (parent process ID). When you start a process using your current shell, the shell itself becomes a parent for your newly started process. The trouble with this approach is that this may not always be desirable, since when the parent process is terminated, its child dies with it.

This means that closing your current shell will terminate the child processes inside of it. This is not ideal in some situations, since it would require you to keep the terminal or SSH session alive in order to refrain from stopping the child processes running within it. After logging out of your terminal, the parent shell is terminated and thus terminating all processes forked from it. In this tutorial, we will explore a few methods to detach a process from its parent on Linux.

In this tutorial you will learn:

  • How to detach child process with disown and nohup default commands
  • How to put a process in the background and recall it to foreground
  • How to detach child process with at and screen installable commands
Detach process from parent on Linux
Detach process from parent on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software nohup, at, screen, disown
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Detach process by putting it into the background

When you execute a command that wants to continue running for a long time, the problem is that your current terminal gets dedicated to the command. You can’t continue using the terminal, and you also can’t close it, because that will end the command that’s currently being run.

If you have a command running, and need to put it into the background to free up your terminal, you can press Ctrl + Z on your keyboard to stop the process. Here’s an example.

$ sleep 10000
[1]+  Stopped                 sleep 10000

To see a list of jobs in the background, use the jobs command.

$ jobs -l
[1]+  1650 Stopped                 sleep 10000

To bring a job back to the foreground, we can use the fg command.

$ fg

If we had multiple stopped jobs, we would need to use a percent sign and specify the job ID to bring the appropriate one back to the foreground.

$ fg %1

Great, now we can put processes into the background and bring them back when we want them later. But what if we want the command to continue executing while it’s in the background? For that, we can use the bg command, followed by an ampersand and the job ID. The following command will make our stopped job resume, while keeping it in the background.

$ bg %1
[1]+ sleep 10000 &

Now we can see that the process is still in the background, but it shows a status of “running” instead of “stopped.”

$ jobs -l
[1]+  1650 Running                 sleep 10000 &

There’s still one thing to keep in mind. You can’t close your current terminal, or these background jobs will close. If you need to close your terminal, and don’t want these commands to stop, then you need to “disown” the job(s). If you only have one job in the background, the following command will work:

$ disown

If you have multiple, then you’ll need to specify the job ID.

$ disown %1

You’ll no longer see the job in your jobs table when you execute the jobs command. Now it’s safe to close the terminal and your command will continue running.

$ jobs -l

You can still keep an eye on your running command by using the ps command.

$ ps aux | grep sleep
linuxco+    1650  0.0  0.0   8084   524 pts/0    S    12:27   0:00 sleep 10000

And if you want to stop the command from running, you can use the kill command and specify the process ID.

$ kill 1650

Now we have seen how to put a running process into the background and recall it later. But we also have the option to just start the command in the background at the beginning. All you need to do is put an ampersand at the end of any Linux command.

$ sleep 10000 &
[1] 1900

Just as before, we can see the job listed with the jobs command. This time, though, the job is already running and we didn’t have to manually start it in the background.

$ jobs -l
[1]+  1900 Running                 sleep 10000 &

Detach process and keep it running with nohup

We’ve already seen in a previous example how we can use the disown command to ensure that the command continues running after we close our terminal. Another option is the nohup command.

nohup tells a process to ignore any SIGHUP (hangup) signals that it receives. The SIGHUP signal is sent to a background job whenever the current terminal is closed. Thus, we can have a job in the background, close the terminal, and still see it running in the background. Here’s an example.

$ nohup sleep 10000 &
[1] 1908
nohup: ignoring input and appending output to 'nohup.out'

We have closed the terminal and reopened it, then ran the following command, which shows the command is still running.

$ ps aux | grep sleep
linuxco+    1908  0.0  0.0   8084   524 ?        S    13:05   0:00 sleep 10000

Using the screen command

Up until now, we’ve covered the default Linux methods for running commands in the background. However, there’s also extra software packages that can be used. Perhaps the most popular would be the screen command. See that link for installation instructions and detailed usage instructions.

Rather than attaching a child process to your current terminal, we can attach it to a different process instead. Attaching a process to a “screen” session will allow us to detach it from our current terminal, thereby permitting us to log out of the SSH session or close the terminal completely, without stopping the running process.

Here is how it works:

  1. Run the screen command to start a new “screen”. Optionally, include the -S option to give it a name.
    $ screen -S mycommand
  2. In the new screen session (attached automatically upon creation), execute the command or script you wish to put in the background.
    $ /path/to/
  3. Press Ctrl + A on your keyboard, and then D. This will detach the screen, then you can close the terminal, logout of your SSH session, etc, and the screen will persist. To see a list of screens, use this command.
    $ screen -ls
    There is a screen on:
    	2741.mycommand	(05/11/2022 01:13:24 PM)	(Detached)
    1 Socket in /run/screen/S-linuxconfig.
  4. To reattach to a screen, use the following command, substituting the number below with the process ID of your own.
    $ screen -r 2741

Detach process with at command

Perhabs an even simpler way to detach your process from the current working shell is by using the at command (see the linked article for installation instructions).

The below example illustrates how to run job, in this case the yes command, and then detaching this process from the current working shell.

$ at now
at> yes > /dev/null
job 2 at Thu May 09 11:12:00 2022

On the first line, we started the at command with time argument now. Next, we type the command we wish to run. Lastly, by pressing CTRL + D key combination we send EOT ( End Of Text ) signal that we are done issuing commands and the job will start immediately. Listing current jobs using jobs command will produce no output which means we are free to disengage from our current shell without stopping any processes.

Closing Thoughts

In this tutorial, we saw how to detach a process from its parent process on a Linux system. The main motivation for detaching a child process is to free up the terminal so that it can be closed or the user can log out from their SSH or telnet session. There are numerous ways to detach a process on Linux, with screen and at probably being the easiest, but not available by default. If you do not want to install extra software, disown and starting jobs in the background will work well.

Comments and Discussions
Linux Forum