The nohup command in Linux, allows the user to run a command without worrying that it’ll get interrupted. Normally, long-running processes that you initiate via SSH not only prevent further input but they can also be shut down when the user logs out. The nohup command prevents this, and allows the command to keep running, even when the user disconnects from the terminal. As I’ll show below, this protection only extends to certain kinds of interruptions and not others. I’ll also list the various scenarios in which it makes sense to use the nohup command.
Background: How Processes Can Be Shut Down
Processes that you initiate in Linux usually complete pretty fast, so you don’t often have to think about them shutting down. But sometimes, you execute a command that takes a long time to complete. Think of a process that initiates a remote backup. Those can last for hours. During that time, you won’t be able to type anything else into the command line. But you can use the “&” character at the end of a command to push it into the background while you continue to work on something else.
But even with a process in the background, it can be interrupted for many reasons. Let’s say you’re done with your work and close down your SSH session. Remember that long-running process you initiated? Gone! When you exit your session, the system sends a special signal to every process you initiated that’s still running called “SIGHUP”. This signal shuts down the process even while it still has work to do. This is what the nohup command is meant to fix.
There are other ways, of course, for a process to shut down, but the nohup command specifically refers to the ones terminated due to the SIGHUP signal.
Using the Nohup Command
Using the nohup command is easy. Just add “nohup” before a command like this:
nohup sleep 60 &
With the above command, the system executes a “sleep” command, that typically locks all input, but this sends it into the background, thanks to the “&” parameter. Executing it looks like this:
You can see from the screenshot, that the system generates a message telling you that it’s ignoring input and appending any output to “nohup.out”. When the command completes, if you’re still on the shell, the system prints a message and creates the nohup.out file like this:
In this particular case, the sleep command doesn’t generate any output, so there’s nothing to show. But many commands send output messages, such as which files are being affected, etc.
Leaving out the “&” Character
You can even use the nohup command without the “&” character sending the process into the background. But this simply means that the process will run in the foreground instead and that you won’t be able to do any other work on the terminal until it is completed. Generally, for long-running tasks, the user always sends it into the background, because who wants to wait around doing nothing for long periods?
But in case you do use nohup while retaining the process in the foreground, you can rest assured that if you close the terminal, or you lose Internet connectivity, or something else happens, the process won’t be interrupted. But as mentioned above, you’ll almost always want to run the command in the background.
Alternatives to the Nohup Command in Linux
If you don’t want to use the Nohup command for whatever reason, here are two alternatives.
Use the “Screen” Command for a Detached Session
Tools like “Screen” allow you to create processes on your server that you can then “detach” from your current session in a way that doesn’t close those processes when you log off. It’s a wonderful tool for anyone who needs to maintain long-running processes. I’ve used the screen command myself, though not to maintain long-running processes, but to separate my work into different tasks.
The difference between using the screen command and nohup command is that the former is simpler and requires no overhead. Just add “nohup” to the command, add a “&” symbol, and the process will run in the background. Nohup is widely used and recognized, and it’ll be compatible with any flavor of Linux.
Screen, on the other hand, takes a bit of getting used to. Along with its greater functionality, comes more complexity, and for this reason, it’s better to use nohup when you just need to send a process into the background and let it run without interruptions.
Disown a Job
Another alternative to nohup is to “disown” a long-running process. The disown command removes the process from the shell’s “job table”, and this keeps it from receiving the SIGHUP signal when the session shuts down. To disown a process, do the following:
sleep 300 &
The above command initiates a “sleep” process for five minutes and sends it into the background.
jobs
This lists the current jobs. If there’s just one job in the background, then that will be the sleep command.
disown %1
The above command disowns the job. Here’s a screenshot showing how it all works together:
You can see that the job shows up and how I disown it. Now if I log off, the sleep command will continue operating in the background, even though, in this case, it doesn’t mean anything.
The difference between disowning a job and using the nohup command is that you can use the former only when you start a job, but you can disown a process whenever you want. Another difference is that nohup allows you to redirect the output of the process to another file. In contrast, if you didn’t initially start a process with the ability to direct its output to another file, then the disown command won’t let you create an alternative output.
The disown command is more of a flexible option when you didn’t think a particular job would take so long. Using nohup is a thought-out process, because you have to use it from the get-go.
Scenarios Where Using nohup Makes Sense
Here are some scenarios where an administrator would find value in using the nohup command.
Data Backups
Backups can take a long time. A slow, remote backup of a simple site of mine takes hours to complete daily. Needless to say, if you initiate this process from the command line, you don’t want it to be interrupted, and it’s a perfect use-case for no-hup
Monitoring
Whether you’re monitoring log files or system health, processes that keep tabs on the system and send their outputs to files are also great candidates for nohup.
Stress-Testing
If you’re running a long stress test of a website, it can last a while. Using nohup to initiate the stress tests so that they don’t stop if you accidentally disconnect is another perfect illustration of what makes it so useful.
Several of these applications are particularly important because you can’t just resume them where they left off without starting all over from the beginning. It’s for these applications that nohup is particularly effective.
Conclusion
As you can see, nohup is easy to use and is valuable in many use-case scenarios. Once you get used to the idea, you can use it to initiate background commands on all Linux systems without worrying about them getting shut down when you log off or exit the shell.
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Sayani Chatterjee says
Hello Mr. Park,
Thank you for the writeup on ‘nohup’ and its alternatives. I am a nohup user for last 12-13 years in Linux (Fedora, Ubuntu, CentOS etc.). Very recently I have started using Fedora 39 in my laptop. As usual, I use nohup for running my programs, which turns out to be oddly slow when I logout. To be precise, it was NOT got killed/ became idle, rather it has been extremely slow (CPU usage time is extremely low) and thereby wasting a huge real time (almost 10 times). No other alternative helped me. I bypassed the problem by switching off the automatic lock screen/ logout options. Could you please shed some light on this odd behaviour of ‘nohup’ and how to resolve the issue?
Thanks and Regards,
Sayani
Bhagwad Park says
Hi Sayani,
Could you try using “disown” command along with nohup and see if that helps? Something like:
nohup your_command & disown
Let me know how it goes!