There are a lot of commands available on Linux systems. There are some you’ll use multiple times a day and others generally reserved for special use cases.
nohup is one such command. You won’t be using it every day, but you’ll be grateful it’s around when you need it. Here we show you what
nohup is and how to use it.
What Is Nohup?
nohup is short for “No Hangups.” It’s not a command that you run by itself.
nohup is a supplemental command that tells the Linux system not to stop a command once it has started. That means it’ll keep running until it’s done, even if the user that started it logs out. It serves a somewhat similar purpose to tmux or screen, allowing you to start a process and not worry about whether it’ll finish if you log out or get disconnected from your server. The syntax for
nohup is simple and looks something like this:
Notice the “&” at the end of the command. That moves the command to the background, freeing up the terminal that you’re working in.
nohup works with just about any command that you run in the terminal. It can be run with custom scripts as well as standard system commands and command line utilities. You might use it to start a security auditing script on a remote server and be able to disconnect and walk away, or you may just want to use it if you want to run updates in the background and not have them stop for anything.
nohup can keep running independently of the user that started it, the command needs somewhere to output any messages or errors. Since there isn’t a terminal to associate with it,
nohup logs everything to an output file,
By default, that file is located in whichever directory you started the command in.
nohup.out is somewhat unique because it contains both the standard output and the error output together.
nohup redirects both to the same file by default.
You don’t necessarily need to use
nohup.out, though – it’s just the default. You can specify a custom output when you run
nohup and place it in a custom location.
The custom output contains exactly the same data as the standard
nohup.out file would. This is nice for the security audit script I mentioned above. You could name it
audit-script-output.txt in some kind of mounted remote folder and view that server’s security posture from the comfort of your own laptop rather than looking through
cat outputs all day.
How Is Nohup Different Than a Daemon?
By this point, you’re probably wondering what sets
nohup apart from a daemonized process. After all, they both do seem to serve the relatively same purpose but really don’t.
Daemons run continuously in the background. They’re best reserved for processes that you don’t want to ever exit, like servers. They require more work to program, too, so they’re not best for simple one-off scripts.
nohup is for a single use. Think of a script that will take a long time to run but will still ultimately finish. Maybe there’s a long and complicated task that you run every now and then that takes hours to complete. You don’t want to leave a terminal open or a user logged in, so you use
nohup to keep it running in the background and put all the output into whatever file you choose, whether the default
nohup.out or your chosen location.
If you enjoyed this writeup on
nohup, its benefits, and its uses, make sure you check out some of our other Linux content, like our explanation of file permissions or speeding up a slow Linux machine.