Read in 3 minutes
Linux Nohup Command
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.
Normally when you run a program over SSH if your connection drops, or you log out the SSH session is terminated and all the processes executed from the terminal will stop. This is where the
nohup command comes handy. It will ignore 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 will run the
mycommand command in the foreground and redirects the command output to the
nohup.out file. This file will be created in the current working directory. If the user running the command doesn’t have write permissions to the working directory the file will be created in the user’s home directory.
If you log out or close the terminal the process will not be terminated.
Running the Command in Background
nohup in the foreground is not very useful because you will not able to interact with the shell until the command completes.
nohup in the background append the
& symbol at the end of the command:
nohup mycommand &
When running the command in the background the shell job ID (surrounded with brackets) and process ID will be printed on the terminal:
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
kill command followed by the process ID:
kill -9 25132
Redirecting Output to a File
nohup will redirect the output to the
nohup.out file. If you want to redirect the command output to a different file you can use the standard shell redirection.
For example to redirect the standard output and standard error to the
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 number of 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 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.
By now you should have a good understanding of how to use the
nohup command to prevent commands from being terminated when you log out or exit the terminal.