• United States
Unix Dweeb

How to keep processes running after logging off in Linux

Oct 20, 20203 mins

When you disown a Linux process in bash, you keep it from being terminated when you log out and allow it to finish on its own. This post shows how to use the disown command.

When you want a process to continue running even after you log off a Linux system, you have a couple options.

One of them is to use the disown command. It tells your shell to refrain from sending a HUP (hangup) signal to the process when you log off. So, the process continues running. This can be very handy whenever you start a process, and then for some reason you can’t stay logged in and wait until it finishes.

The disown command is a shell built-in. This means that you don’t have to install it to use it, but it also means that it won’t be available if you use a shell that doesn’t support it. For those of us using bash and related shells (zsh, ksh etc.), disown should be available, and you can verify this with a command like the following that lists shell built-ins and then looks for “disown”:

$ show_builtins | grep disown
 disown [-h] [-ar] [jobspec ... | pid >  test [expr]

Unlike nohup, which has pretty much the same effect, disown is used after you’ve started a process. Just specify the process ID with the disown command:

$ ps -ef | grep long-loop
shs     799217  1  0 11:04 ?   00:00:00 /bin/bash /home/shs/bin/bigjob
$ disown 799217

The process will continue running after you log off, and if it hasn’t finished by the time you log in again, will still be running until it’s completed. In fact, it won’t even be affected when you log off again because it will not be associated with your current shell.

Check out disown

If you’d like to see how disown works, you can set up a simple loop in a script. Here’s an example:


while true
   date >> my.log
   sleep 600

This script adds the current date and time to a file named “my.log” every 10 minutes and has no stopping point. You can start it in the usual way:

$ ./my-loop

Then, when you’re ready to log off you can maybe run off somewhere, suspend your process with ^z (hold control key and press “z”). After that, list your processes:

$ ps
    PID TTY          TIME CMD
 801593 pts/3    00:00:00 bash
 801812 pts/3    00:00:00 long-loop
 801816 pts/3    00:00:00 sleep

Then use the disown command with the script’s process ID:

$ disown 801812

Note that, if you run your process in the background from the start (e.g., my-loop &), you don’t need to use the ^z.

Terminating a disowned process

Most processes will not, of course, be designed to run forever. They’ll probably finish before you log back in again. In the case of this example loop, you would eventually need to use a bit of force to stop it once you’ve logged off and back on. The “sure kill” -9 option should do this for you:

$ kill 801812
$ ps
    PID TTY          TIME CMD
 801593 pts/3    00:00:00 bash
 801812 pts/3    00:00:00 long-loop     

Some options

You might have noticed in the output of the show_builtins command above that the disown command has several options. The -a option will disown all backgrounded processes while -r means it will only disown running (not stopped) processes. In both cases, the jobs being disowned will no longer show up when you type "jobs". When you use the -h option, on the other hand, the job will not removed from the jobs list, though the shell will still refrain from sending an HUP signal to it when you log out.

Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author