How to use Linux nohup command to keep processes running
Using the Linux command nohup, your system ignores the hangup command HUP. This lets processes continue to run in the background. Its output is then redirected to the file nohup.out or another file of your choice.
What is Linux nohup?
To understand what the Linux nohup command is, it’s worth looking at SIGHUP. This is a signal that is generated under Linux, among other things, when there’s a connection failure on a data line between the operating system and another application. Under certain circumstances, however, it’s necessary for this signal not to be sent. This is the case if an application is to continue running even after the user has logged off from the system. To suppress the signal and allow programs to continue running in the background, Linux nohup is used.
How does the Linux nohup command work?
Linux nohup is included in all common Linux distributions like Debian and Ubuntu and therefore doesn’t need to be installed separately. You can use it to instruct the system to run a specific program in the background, thus detaching it from the login shell. Any output is automatically redirected to the nohup.out folder. For longer processes this has the advantage that you can log off without interrupting the process.
What does the Linux nohup syntax look like?
The basic syntax of the Linux nohup command always looks the same:
$ nohup [Command] [Argument] [&]
bash“[Command]” and “[Argument]” are parameters that can be customized. To allow processes to continue running in the background after logging off, use “[&]”.
What options does the nohup command have?
Linux nohup doesn’t accept any options other than the two standards “-h” or “–help” and “-v” or “–version”. Only when refraining from entering a parameter for “[command]”, is the option “-p” or “–parents” allowed.
Examples for the Linux nohup command
Finally, to help you understand how the command works, we’ll show you a few examples.
$ nohup bash example.sh
bashThis executes the command in the foreground. The output of the command is automatically redirected to the file nohup.out.
$ nohup bash example.sh > output.txt
bashThis command redirects the output to the output.txt document.
$ nohup bash example.sh &
bashBy using this input, the command will persist and continue execution in the background. If you wish to reverse this and bring the command back to the foreground, you can execute the fg command.