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. 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, returning you to the prompt on the terminal you’re working on. Many times the command will still remain attached to the terminal, so you’ll terminate it the moment you close the open window and precede the command with nohup. 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. Use it to start a security auditing script on a remote server so that you can disconnect and walk away or to run updates in the background and not have them stop for anything. Tip: check out other ways to run bash commands in the background.
Nohup.out
Because nohup can keep running independent of the user that started it, the command needs somewhere to output any messages or errors. As there isn’t a terminal to associate with it, nohup logs everything to a “nohup.out” output file By default, that file is located in whichever directory you started the command. “nohup.out” is somewhat unique, as it contains both the standard output and the error output. 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 the same data as the standard “nohup.out” file and is nice for the security audit script 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. 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 just append itself with all outputs, creating a bit of a mess. If you want to read command output in an organized manner, specifying custom file names for each output gives you a means to do that in your own way!
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 relatively the same purpose but don’t really. 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 more of a single-use command. Think of it as 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 use nohup to keep it running in the background and put all of the output into whatever file you choose, whether the default “nohup.out” or a location of your choice.
Running a Process With Nohup
To run a process with nohup, put it before any command you run. This includes all normal programs you would run, such as Discord, Steam, your browser, a text editor, or any other application you can think of. Remember, you must detach your process from the active prompt so that you can close your terminal without suddenly losing the process. For example, to open Kate (my text editor) on my system and detach it from the terminal while keeping a log with “kate.log,” I would type this: After closing the terminal, Kate is still running. If there were a problem starting the application, I could simply pull up “kate.log” and figure out what’s wrong directly from the terminal with: This process is useful to determine whether there’s an issue with an application that refuses to start or behaves erratically. When you typically start a program like your browser, its error signals are often ignored. Starting them in the terminal with nohup and passing the output onto a logfile helps paint a clearer picture of what could be causing problems.
Running Multiple Commands With Nohup
Since you can pass any command through nohup, you can also pass a command sequence. To do this correctly, you’ll have to invoke a shell first and pass the commands along as a string, as most terminal emulators interpret every command after the first as not being preceded by nohup. Every modern Linux distro has bash installed, so we are ignoring other custom shells and using that one for this example: In this command, we invoked bash to execute a command and chained them with a double-ampersand (&&). We’re telling nohup to “Run bash with the -c (execute) flag and pass this string surrounded by single quotes to it.” Then we tell the terminal to free up the prompt while that entire sequence runs in the background. Screenshots for “What Is Nohup?” and “Nohup.out” by John Perkins, and those for “Running a Process With Nohup” by Miguel Leiva-Gomez. This removes the running process completely from the terminal’s job list. In some cases, a process you start with nohup and the ampersand will continue to request input and listen for signals from the terminal. When the terminal closes, if the application is still in the job list, it will also send signals to it. The application will also close if it considers the terminal’s signal its cue to close. Disowning it ensures that this doesn’t happen. It’sna murky and dangerous territory for root access. If you insist on running a command through nohup as root, do so after entering the root shell by typing su in your terminal and going through with it. You’ll often find that applications that want your root password won’t react well to this. What this means is that you will no longer be able to send terminal input to the command you just ran, and all the data that would normally stream through your terminal will be redirected to one single stream (stdout), then output into a file (by default, “nohup.out”).