Linux nohup Command Tutorial

Linux nohup command is used to run processes that ignore the SIGHUP signal. The SIGHUP is a signal used to send to a process and control it even the terminal is closed. By default, a process is stopped when the executing terminal or sessions is closed but by using the nohup command this can be prevented.

nohup Command Help

Even the nohup command provides only two options the help information can be displayed with the –help option. The help option also lists some standard input and standard output related information about nohup.

nohup --help
nohup Command Help

nohup Command Syntax

The honup command has very simple syntax. The command we want to run and its options, parameters are provided after the nohup command.

  • COMMAND is the command or script we want to run even the terminal or sessions is closed. This is required.
  • OPTIONS is the options or parameters used with the COMMAND. This is optinal.

nohup Command Example

Lets make an example where the provided command runs even we closed the terminal screen or disconnect from the remote session like SSH. In the following nohup example we will the top command.

nohup watch /home/ismail

When this command is executed the following output is printed. As commands and scripts have some input and output the current watch command output is automatically redirected into the “nohup.out“. the output is appended into the nohup.out file which preserves previous output.

nohup: ignoring input and appending output to 'nohup.out'

Running Command or Script in Background

By default, the executed command or script runs in the foreground. Even we close terminal or SSH sessions it will continue the execution but alternatively, we can run the command in the background by default. The bash shell & ampersand is used to run command or script in the background and will continue event the current terminal or SSH session is closed. The & (ampersand) added after the command.

nohup watch /home/ismail &

Redirect Output to a File

By default, the executed command or script output is redirected into the file named “nohup.out”. The output is appended into this file without overwriting the previous content. We can explicitly redirect the command or script output into the specified file easily. We will just use the > sign to redirect the output into a file and provide the file name and path. In the following example, we redirect the output into a file named watch.output which is located under the /home/ismail.

nohup watch /home/ismail > /home/ismail/watch.output

Kill or Terminate nohup Process

As the nohup command runs the provided command or script unless the system is rebooted or shut down we may need to kill or terminate this nohup command or script explicitly. First we will list currently running process and its process ID or PID with the ps command like below.

ps aux | grep watch

We can see that the process ID is 5385. Now we will kill this process with the kill command like below.

kill -9 5385

Alternatives to nohup

The nohup command provides easy and practical way to run commands and scripts even after closing the terminal or SSH session. But there are some alternatives which are generally soft terminals do not killed after closing terminals or disconnecting SSH sessions.

  • Screen
  • Tmux
READ  What Will Happen When Delete Root File System with "rm -Rf /"?

Leave a Comment