How to Use Linux Nohup Command

Introduction

Before we begin talking about how to use Linux Nohup Command. Let’s briefly understand - What is a Nohup?

The nohup command mainly executes another program which is specified as its argument, and it ignores all SIGHUP (hangup) signals. The SIGHUP is a signal sent to a process when its controlling terminal gets closed.

If your connection drops, or you log out accidentally while running any program over SSH, then all the programs executed from the terminal will stop. It is when nohup command comes into action as it ignores all hangup signals and the process will continue running.

In this tutorial, you will use Linux nohup Command. We will also address some of the FAQs relating to nohup Command.

Advantages of nohup Command

  1. Background Execution: Nohup allows running commands in the background even after logging out, ensuring uninterrupted execution.
  2. Output Logging: It saves command output in a file, preventing interruption of ongoing processes.
  3. Process Immunity: Nohup protects processes from hangups and terminates them only when execution is complete.
  4. Flexibility: It allows combining multiple commands, pipelines, and redirections with ease.
  5. Simplifies Management: Nohup simplifies process management by detaching them from the terminal and preventing accidental termination.

Step 1 - Using the Nohup Command

1) Firstly, the syntax for nohup command is as follows:

nohup COMMAND [ARGS]

2) The command does not accept any other options except the standard --help and --version.

Take a look at the below example:

nohup mycommand
Output

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

nohup will run the mycommand command in the foreground and will redirect the command output to nohup.out file. The file will generally be created in the current working directory and if the user doesn't have permissions to the working directory then the file will be created in the user's home directory.

Even after logging out or closing the terminal, the process will not be terminated.

Step 2 - Running Command in Background

1) You will not be able to interact with the shell until the command completes, so there is no use in running it in the foreground.

If you want to run the command in the background, append the & the symbol at the end of the command:

nohup mycommand &

The output will include the shell job ID which is surrounded by brackets and a process ID:

Output

[2] 14356

2) To bring the command in the foreground, you can use the job ID with fg command.

If you want to terminate the process, you should use kill command followed by process ID:

kill -9 39227

Step 3 - Redirecting the Output to a File

1) By default, the command output is redirected to the nohup.out file. If you wish to redirect the output to a different file, you need to use the standard shell redirection.

For example, to redirect standard output and standard error to mycommand.out you need to use the following command:

nohup mycommand > mycommand.out 2>&1 &

2) If you want to redirect standard output and standard error to different files, use the following command:

nohup mycommand > mycommand.out 2> mycommand.err &

Other Alternatives

There are many alternatives that you can use to avoid a command being terminated in case you get disconnected or close the terminal.

1) Screen

The Screen or GNU Screen is basically a terminal multiplexer program. It allows you to start a screen session and an open number of windows, i.e. virtual terminals, inside that session. So, the processes running in the Screen will continue to run as is even if their window is not visible, or you get disconnected.

2) Tmux

The Tmux is a modern alternative to the GNU screen. With Tmux, you can create a session and open multiple windows inside that session. Tmux's sessions remain persistent, which simply means the programs in Tmux will keep running even if your terminal is closed.

3) Disown

disown is a shell built-in that removes a shell job from the shell’s job control. Unlike, you can use disown on running processes too.

FAQs on the Linux Nohup Command

Why should I use the nohup command? 

The nohup command ensures that your processes or commands keep running in the background even if you log out, preventing them from being terminated.

Can I redirect the output of a nohup command to a file? 

Yes, by using the > or >> operators, you can redirect the standard output of a nohup command to a file for later analysis.

Can I start multiple nohup processes simultaneously? 

Yes, you can start multiple nohup processes by running each command with the nohup prefix and an ampersand at the end.

Can I use nohup with commands that require user interaction? 

No, the nohup command is designed for non-interactive commands. For commands that require user input, consider using tools like screen or tmux.

Is nohup available on all Linux distributions? 

Yes, the nohup command is available on most Linux distributions and is part of the POSIX standard. You can typically find it pre-installed or install it easily using package managers like apt, yum, or dnf.

How can I retrieve the output of a nohup command? 

The output generated by a nohup command is saved in a file named nohup.out in the same directory. You can check this file later to view the command's output.

Can I run a nohup command on a remote server? 

Yes, you can run a nohup command on a remote server by connecting to the server using SSH and then executing the command with nohup.

Conclusion

We hope this detailed tutorial helped you understand how to use Linux Nohup Command.

If you have any queries or doubts, please leave them in the comment below. We'll be happy to address them.