Nohup Command in Linux

In this tutorial, you will come across various methods and techniques of working with the nohup command in Linux.

Nohup Command in Linux

The nohup command or "no hangup" is a Linux command used to run a command or a script in the background without interruptions, even if the user who started the command logs out of the system.

The nohup command is often used to start long-running processes, such as server processes, that require resources to run even after the user session is terminated.

Linux nohup Command

The basic syntax for using the nohup command is as follows:

nohup command [arguments] &

Where the command is the program or script, you wish to run in the background, and arguments denote the parameters to pass to the command to modify the behavior of the nohup command.

The ampersand symbol& at the end of the command is used to tell the system to run the process in the background instead of the foreground.

Take for example the command below that runs a python script in the background using the nohup command:

nohup python3 &

The command above will run the python script in the background and put any output, logs, and error messages in the nohup.out file. An example output is as shown:

[1] 3196
appending output to nohup.out

The nohup.out file is created in the current working directory. If you wish to store the output in a different directory, you can use the -o option as shown in the example below:

nohup -o processes.out python3 &

This will allow the nohup command to store the output in the processes.out file.

If you want to redirect the standard output (stdout) and standard error (stderr) of a command to different files when using the nohup command, you can use the > and 2> operators to redirect each stream to a separate file.

nohup python3 > std.out 2> std.err &

The command above will redirect the standard out to std.out file and the standard error to std.err file.

Alternatively, you can use the &> operator to redirect both stdout and stderr to the same file.

For example:

nohup python3 &> logs.out &

This can be useful if you combine both streams' output into a single file for easier monitoring and analysis.

Points to Note

One important thing to note about the nohup command is that it does not prevent the process from being terminated when the system is shut down or restarted. In such as case, you can look for alternatives such as tmux and screen commands.

If you enjoy our content, please consider buying us a coffee to support our work:

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.