How to use the nohup command in Linux

The no hang up or nohup command allows processes in your Linux system to keep running even after the terminal closes. This is essential for system administrators, as it helps prevent long-running tasks like builds or monitoring from being interrupted.
Using the nohup command is straightforward: simply enter it in your system’s terminal, followed by the process you want to run continuously. Beyond basic usage, you can use it to run tasks in the background and redirect output to a specific file.
In this article, we’ll cover usage of the nohup command in more detail, as well as its real-world applications.
What does the nohup command do?
Linux sends the hang-up signal (SIGHUP) to terminate all processes in the shell when you close the terminal. This can also happen in a remote system due to connectivity issues.
To restart the processes, you must manually reenter the Linux commands or scripts. This can be tedious and time-consuming if there are many long-running tasks.
The nohup command blocks the processes from receiving the SIGHUP signal, keeping them running even after terminal exit.
What are the use cases for nohup?
The nohup command is helpful for maintaining continuous tasks. Here are some examples of popular use cases for system administrators and developers:
- Maintaining long-running processes. Long-running jobs like data processing or software builds take a very long time and require a complete restart when interrupted. The nohup command prevents them from stopping after logout or SSH disconnection.
- Server maintenance. The use of nohup allows server maintenance scripts – like log rotation, updates, or cleanup tasks – to run independently of an administrator’s session. This keeps the system functioning reliably.
- Running automatic backups. You can use nohup to run scripts that regularly back up your server data even when you are not logged into the shell. This is a simple way to ensure data integrity during long or unattended operations.
- Automation and scheduled tasks. Scheduled tasks like deployments or report generation can run independently using nohup. This allows developers and sysadmins to run persistent scripts without setting up a full service manager like systemd.
nohup command syntax
The nohup command syntax is structured as follows:
nohup command [options]
Here’s the breakdown of the syntax:
- nohup – the command name, instructing the terminal to enable no hang up.
- command – the command, script, or process you want nohup to run continuously.
- [options] – optional flags for modifying the nohup command behavior.
The nohup command options are as follows:
- –help – displays the nohup command manual in your system’s shell.
- –version – outputs the nohup command version, useful for checking if it’s installed and up-to-date.
- & – an operator for running the command, script, or process in the background instead of the main shell.
You can run the following command to check the nohup version:
nohup --version
How to use the nohup command?
Here are several nohup command examples in a real-world scenario. To follow along, open your system’s terminal or the browser terminal if you use a Linux virtual private server (VPS) from Hostinger.
Running a process with nohup
To run a process indefinitely, use the basic nohup command syntax as follows:
nohup process
For example, enter the following to run a simple Bash shell script that prints a Hello world! message:
nohup ./script.sh
After running nohup, you can see the process’s output from the nohup.out file. Check its content using the following cat command:
cat nohup.out
Redirecting output to files
To save the output of processes run using nohup to another file, use the redirection operator (>) like so:
nohup ./script.sh > file.txt
Check the saved output by printing the file’s content using the cat command:
cat file.txt
Note that the above redirection operator will overwrite the file with the new output. If you want to append the existing content, use the following operator:
nohup ./script.sh >> file.txt
In some cases, nohup doesn’t automatically redirect errors, meaning you must do it manually. For example, the following will log the process’s output and errors to file.txt:
nohup ./script.sh > file.txt 2>&1
Here’s the breakdown of the command:
- nohup ./script.sh > file.txt – the command for redirecting the nohup process output to file.txt.
- 2 – the identifier for stderr or errors from the process.
- 1 – the identifier for stdout or output from the process.
- >& – the output redirection that writes errors (2) to wherever the output (1) will go, in this case, file.txt.
In the Bash shell, the command can also look like the following:
nohup ./script.sh &> file.txt
If you want to append errors, simply use the >> redirection instead of > like so:
nohup ./script.sh >> file.txt 2>&1
You can also save the nohup process’s output and errors in separate files like so:
nohup ./script.sh > output.txt > errors.txt
Mind the gap
Note that there’s no space between the redirection operator and stdout or stderr.
Running processes in the background
By default, the nohup command will run a process in the main terminal shell. This means that you must terminate it to run other utilities, which can be problematic if you have multiple tasks.
As a workaround, nohup provides the & option to let you run the process in the background. This enables you to use the main shell for other tasks even when the process is running. Here’s how the command looks:
nohup process &
When running nohup in the background, your terminal will give an output similar to this:
[1] 1234
The [1] placeholder represents the background process’s order in the current terminal shell. For example, [1] means the first, [2] is the second, and so on.
Meanwhile, 1234 is the job’s process ID (PID). This information is essential for identifying and stopping the process, which we’ll explain later.
Checking the nohup process status
To check running processes started using nohup, use the ps command followed by their PID. Here’s the command:
ps -p PID
The process is running if the terminal outputs the executed command or script name.
If you forget the PID, use pgrep to check a nohup process’s status using its command or script name, like so:
pgrep -a process-name
For example, enter the following if you want to check if the sleep command is running:
pgrep -a sleep
If you forget both the PID and process name, use the lsof command to check jobs that use the nohup output file:
lsof | grep nohup.out
Your terminal will list commands and scripts run using nohup, along with their PID. Remember to replace nohup.out if you redirected the output to another file.
How to manage nohup processes
To stop a nohup process that is running in the main shell, hit the Ctrl + C shortcut on your keyboard to send the SIGTERM signal – a graceful termination method that allows the task to finish first before stopping.
If you run a nohup task in the background, the only way to stop it is to use the kill command, which lets you kill a process in Linux using its PID. The syntax is as follows:
kill PID
For example, here’s a command to terminate a process with a PID of 1234:
kill 1234
By default, the kill command will send the SIGTERM signal, which may not be enough to stop an unresponsive process. In this case, use the SIGKILL signal to forcefully terminate the task.
To pass the SIGKILL termination signal using the kill command, add the -9 option:
kill -9 PID
Note that you can’t bring nohup processes from the background to the foreground or the main shell. This is because nohup isn’t interactive and doesn’t register your tasks as jobs, which are manageable using the fg or bg command.
If you wish to interact with background processes, run them using alternative tools that we’ll explain in the next section.
What are the alternatives to nohup?
In addition to nohup, Linux has other tools that let you run commands in the background. Here are some popular options.
GNU Screen
The GNU Screen is a terminal multiplexer that enables you to create virtual shell sessions that remain active in the background.
Since Screen is interactive, it’s an excellent alternative to nohup if you wish to manage and switch between processes easily.
tmux
Terminal multiplexer (tmux) is a more modern and user-friendly Screen alternative. This tool has features like better window splitting and an intuitive user interface, which makes it easier to use than Screen.
However, tmux’s deeper functionality also makes it more challenging to learn than Screen.
systemd-run
The systemd-run command runs processes as a service, which is more reliable because your tasks have better isolation, dependency handling, and failover mechanisms.
However, systemd-run is only available in systemd-based operating systems. It’s also more complicated to set up than nohup since you must configure the process as a service.
Docker
Docker is a containerization tool for running processes in dedicated environments, with their own resource constraints and software setup. Since tasks can run independently from others, it provides a high level of isolation and reliability.
Docker is excessive for simple tasks that you usually run with nohup because it can be challenging to set up and manage. It’s ideal for complex processes, like setting up a consistent deployment pipeline.
Conclusion
The nohup command is a handy tool for keeping simple processes running in the background. It is especially useful when working with a remote server where a connection issue may end the terminal session.
Note that nohup is most suitable for simple, one-off processes. For more complex tasks, consider an alternative tool with interactive functionality like Screen.
Once you’ve grasped the purpose of the nohup command, try it on your system’s terminal or in Hostinger’s VPS browser terminal. Hostinger’s VPS hosting also gives you the option to ask Kodee, an AI assistant, for help if you need it.
All of the tutorial content on this website is subject to Hostinger's rigorous editorial standards and values.