Linux Commands | Process Management | nohup Keep Running After the Session Ends

Basic nohup usage, standard output handling, and background execution

nohup Command

The nohup command lets a process continue running even if the terminal session is disconnected, such as when the user logs out.

When a terminal session is disconnected, Linux normally sends a HUP (Hang Up) signal to the processes started from that session so that they terminate. The nohup command means “do not send the HUP signal to a process that should keep running even after the session ends” (No Hang Up).

Basic nohup Usage

The basic command is as follows.

nohup [command]

To quickly learn how to use nohup, create a simple script and run it with the nohup command.

Create a Script

Create the script to run with nohup as shown below.
test.sh

#!/bin/bash

for i in {1..10}
do
  sleep 5
done

This script runs the loop 10 times. Because it pauses for 5 seconds with sleep 5 in each iteration, it finishes after a total of 50 seconds.

Change Script File Permissions

The command file executed with nohup must have permissions of at least 755 (rxwrx-rx-), so set the script permissions to 755.

chmod 755 test.sh

Run the Script with nohup

When you run the script with nohup, a message like the following appears.

% nohup ./test.sh
appending output to nohup.out

When run this way, the process remains in the foreground and exits if you press Ctrl+C.
The script’s standard output is written to a nohup.out file in the directory where nohup was run.

Write nohup Standard Output to Another File

Use redirection (>, >>) to write standard output to another file.

nohup ./test.sh > nohup_test.out

If you do not want to write standard output at all, use the following command.

nohup ./test.sh > /dev/null

Standard Output and Standard Error

Sometimes you need to write standard output and standard error to separate files, and sometimes you want to write both to the same file. The following examples cover both cases.

0 : standard input
1 : standard output
2 : standard error

Write standard output and standard error to separate files

nohup ./test.sh 1 > test.out 2 > test.err

This means standard output (1) is redirected to the test.out file, and standard error (2) is redirected to the test.err file.

Write standard output and standard error to the same file

nohup ./test.sh > test.log 2>&1 &

This means standard output is written to test.log, and standard error (2) is redirected to the same file used by standard output (1).

Run nohup in the Background (&)

Using & runs a program in the background.

% ./test.sh &

In this case, the terminal does not remain waiting even if you do not press Ctrl+C.

You can confirm that it is running in the background with the following command.

% ps -ef | grep test.sh

What is the difference between nohup and &?
The nohup command runs a program in a daemon-like form, so as described above, the process continues running even if the session is disconnected, such as after logout. However, when started this way, it remains in the foreground, and pressing Ctrl+C terminates the process immediately.
By contrast, background execution (&) does not leave the terminal waiting, but if the session is disconnected, the program that was started also terminates.

If you run nohup together with background execution (&) as follows, the process runs in the background without waiting, and it continues running even if the session is disconnected.

% nohup nohup ./test.sh > /dev/null &

The summary is as follows.

Command Ctrl+C Terminal Closed
[command] Command stops Command stops
[command] & Command does not stop Command stops
nohup [command] Command stops Command does not stop
nohup [command] & Command does not stop Command does not stop

Terminate the Process

To terminate the process, first look up the process ID with the following command.

% ps -ef | grep test.sh
  501 61324 21348   0  7:34AM ttys001    0:00.01 /bin/bash ./test.sh
  501 61329 21348   0  7:34AM ttys001    0:00.00 grep test.sh

Then terminate the process ID you found with the kill command.

% kill 61324