What Is Nohup and How Do You Use It?

There are a lot of commands available on Linux systems. There are some that you’ll use multiple times a day, and there are 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.

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 another command once it has started. That means it’ll keep running until it’s done, even if the user that started it logs out. 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.

Because 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, nohup.out.

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.

Nohup Custom Output

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.

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. As an added bonus, it’ll automatically log everything for you.

You can see that nohup isn’t an everyday tool, but it can be very useful for the right task. It falls somewhere between a normal short script and a continually running daemon. It helps you to take more of a hands-off approach to longer scripts that you might have regularly wasted time babysitting.

Leave a Reply

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.