There are many commands available on Linux systems. Some you will use several times a day, while others are usually reserved for special uses. Nohup is one such command. You won’t use it every day, but you’ll be grateful that it’s available when you need it. Here’s what nohup is and how to use it.
What is Nohup?
nohup is short for “No Hangups”. It is not a command that runs by itself. nohup is a supplemental command that tells the Linux system not to stop a command once it has been started. It will continue to run until it is ready, even if the user who started it logs off. It has a somewhat similar purpose to tmux or screen, allowing you to start a process and not worry about whether it will end if you log out or disconnect from your server.
The syntax of nohup is simple and looks like this:
Note the & at the end of the command. That moves the command to the background, returning you to the message in the terminal you’re working on. Many times the command will still be attached to the terminal, so it will end it the moment you close the open window and precede the command with nohup.
Nohup works with almost any command you run in the terminal. It can be run with custom scripts as well as standard system commands and command-line utilities. Use it to start a security audit script on a remote server so you can disconnect and zoom out or to run updates in the background and not make them stop for anything.
Tip: Check out other ways to run bash commands in the background.
Nohup.out
Because nohup can continue to run regardless of the user who started it, the command needs a place to generate any messages or errors. Since there is no terminal to associate with it, nohup logs everything in an output file “nohup.out”
By default, that file is located in the directory where you initiated the command. “Nohup.out” is somewhat unique in that it contains both standard output and error output. Nohup redirects both to the same file by default.
However, you don’t necessarily need to use “nohup.out”, it’s just the default. You can specify custom output by running nohup and place it in a custom location.
The custom output contains the same data as the standard “nohup.out” file and is nice for the security audit script mentioned above. You could call it “audit-script-output.txt” in some kind of mounted remote folder and see that server’s security posture from the comfort of your own laptop instead of looking through cat outputs all day.
Even for normal everyday users, specifying a custom output file could be useful as it would allow you to have a clear distinction between outputs. If you use this command multiple times from your default (home) folder without specifying an output file, “nohup.out” will simply be added with all the outputs, creating a bit of clutter. If you want to read the output of the command in an organized manner, specifying custom file names for each output gives you a means to do it your way.
How is Nohup different from a demon?
At this point, you’re probably wondering what differentiates nohup from a demonized process. After all, both seem to serve relatively the same purpose, but they don’t.
Daemons run continuously in the background. It’s best to reserve them for processes you never want to leave, such as servers. They also require more work to program, so they’re not the best for simple, one-off scripts.
Nohup is more of a one-time command. Think of it as a script that will take a long time to execute, but will eventually end. Maybe there’s a long and complicated task that you run from time to time that takes hours to complete. You don’t want to leave a terminal open or a user logged in, so use nohup to keep it running in the background and put all the output into any file you choose, whether it’s the default “nohup.out” or a location of your choice.
Run a process with Nohup To run
a process with nohup
, place it before any commands you run. This includes all the normal programs you would run, such as Discord, Steam, your browser, a text editor, or any other application you can think of.
Remember, you need to separate your process from the active message so that you can close your terminal without suddenly losing the process.
For example, to open Kate
(my text editor) in my system and separate it
from the terminal while keeping a record with “kate.log”, I would write this: After closing the terminal,
Kate continues to function.
If there was a problem launching the application, you could simply open “kate.log” and find out what is wrong directly from the terminal with:
This process is useful for determining if there is a problem with an application that refuses to start or behaves erratically. When you normally launch a program like your browser, its error signals are often ignored. Launching them in the terminal with nohup and passing the output to a log file helps paint a clearer picture of what might be causing problems.
Running multiple commands with Nohup
As you can pass
any command through nohup, you can also pass a script. To do this correctly, you’ll first have to invoke a shell and pass the commands as a string, as most terminal emulators interpret each command after the first as if it wasn’t preceded by nohup.
Every modern Linux distribution has bash installed, so we’re ignoring other custom shells and using that one for this example:
In this command, we invoke bash to execute a command and chain them together with a double ampersand (&&). We are telling nohup to “Run bash with the -c flag (execute) and pass this string surrounded by single quotes.” Then we tell the terminal to release the message while that whole sequence runs in the background.
Frequently asked
questions screenshots of “What is Nohup?” and “Nohup.out” by
John Perkins, and those of “Run a process with Nohup” by Miguel Leiva-Gómez.