Linux Nohup Command
3 min read
nohup command executes another program specified as its argument and ignores all
SIGHUP (hangup) signals.
SIGHUP is a signal that is sent to a process when its controlling terminal is closed.
Usually, when you run a program over SSH, if your connection drops or you log out, the session is terminated, and all the processes executed from the terminal will stop. This is where the
nohup command comes in handy. It ignores all hangup signals, and the process will continue to run.
How to Use the nohup Command
The syntax for the
nohup command is as follows:
nohup COMMAND [ARGS]
The command doesn’t accept any other options except the standard
Let’s take a look at the following example:
nohup: ignoring input and appending output to 'nohup.out'
nohup runs the
mycommand command in the foreground and redirects the command output to the
nohup.out file. This file is created in the current working directory
. If the user running the command doesn’t have write permissions to the working directory, the file is created in the user’s home directory.
If you log out or close the terminal, the process is not terminated.
Running the Command in Background
nohup in the foreground is not very useful because you won’t be able to interact with the shell until the command completes.
To run the command in the background
, append the
& symbol at the end of the command:
nohup mycommand &
The output includes the shell job ID (surrounded with brackets) and process ID:
You can use the job ID to bring the command into the foreground using the
If for some reason you want to terminate the process, use the
followed by the process ID:
kill -9 25132
Redirecting Output to a File
nohup redirects the command output to the
nohup.out file. If you want to redirect the output to a different file, use the standard shell redirection.
For example, to redirect the standard output and standard error
mycommand.out you would use:
nohup mycommand > mycommand.out 2>&1 &
To redirect the standard output and standard error to different files:
nohup mycommand > mycommand.out 2> mycommand.err &
There are several alternative programs that you can use to avoid a command to be terminated when you close the terminal or get disconnected.
Screen or GNU Screen is a terminal multiplexer program that allows you to start a screen session and open any number of windows (virtual terminals) inside that session. Processes running in Screen will continue to run when their window is not visible even if you get disconnected.
Tmux is a modern alternative to the GNU screen. With Tmux, you can also create a session and open multiple windows inside that session. Tmux sessions are persistent, which means that programs running in Tmux will continue to run even if you close the terminal.
disown is a shell builtin that removes a shell job from the shell’s job control. Unlike
nohup, you can use
disown on running processes too.
nohup allows you to prevent commands from being terminated when you log out or exit the terminal.
If you have any questions or feedback, feel free to leave a comment.