Americas

  • United States
sandra_henrystocker
Unix Dweeb

How to keep a process running on Linux after you log off

How-To
Oct 10, 20233 mins
Linux

The nohup command can allow a long-running process to run to completion even after you log off.

Black flat screen computer monitor with green "matrix" screen image

By default, processes run on the Linux command line are terminated as soon as you log out of your session. However, if you want to start a long-running process and ensure that it keeps running after you log off, there are a couple ways that you can make this happen. The first is to use the nohup command.

Using nohup

The nohup (no hangup) command will override the normal hangups (SIGHUP signals) that terminate processes when you log out. For example, if you wanted to run a process with a long-running loop and leave it to complete on its own, you could use a command like this one:

% nohup long-loop &
[1] 6828
$ nohup: ignoring input and appending output to 'nohup.out'

Note that SIGHUP is a signal that is sent to a process when the controlling terminal of the process is closed.

Once you use the nohup command in this way, you can log out knowing that it will run to completion on its own. Depending on the process, it is likely to be done the next time you log in. In any case, output that is generates will be added to a file named “nohup.out”.

If you want your output to be put into a separate file (e.g., if you need to preserve some earlier nohup.out content), you can specify another output name with a command like this:

$ nohup long-loop &> nohup2.out &

If the process is still running when you log back in, you should be able to find it in the ps command output. You will not see it using the jobs command.

$ ps -ef | grep loop
shs         6861    6366  0 13:52 pts/4    00:00:00 /bin/bash ./long-loop

If the process is completed before you log back in, looks for its output in the nohup.out file.

Leaving a process to run on its own after it has already started

You can leave a process running on its own even after it has already started running. To do this:

  • Type Ctrl+Z to pause the program and get back to the shell
  • Use the bg command to move the process to run in the background
  • Use the disown -h command (i.e., disown -h %1) where %1 is the job number to disassociate it with your current shell

NOTE: You can also find your job number with the jobs command.

$ big-loop
^Z
[1]+  Stopped                 big-loop
$ bg
[1]+ big-loop &		

Wrap-up

Moving a lengthy process to run in the background can allow you to let it continue when you need to move onto some other task or just go home at the end of a busy day. An earlier post on keeping processes running after you log off is available at this URL:

How to keep Linux from hanging up on you

sandra_henrystocker
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