The nohup command in Linux can help a process immune itself from the HUP signals. If you decide to log out of your system in the middle of any process, the nohup command will make sure it keeps running.
Having control over future events is something not practical. With that being said, it is pretty probable to find yourself in a situation when you need to log out of your Unix system while it runs a critical process. This is when the nohup command comes into play.
The nohup or no hangup is a command-line utility that ignores the HUP signal associated with shell termination actions. Ignoring that, nohup helps any running process to continue even after a user disconnects from the current shell.
Generally, when we log out of our system, the processes also stop operating. The nohup command immune those processes and help keep them executing. In this article, we’ll tell you how.
nohup Command Syntax
The basic form of nohup command in Linux looks something like this:
$ nohup command arguments
The same can also be expressed as:
$ nohup options
You can also employ the
--version flag to check the information about the nohup version that your system is using.
$ nohup --version
Getting Help Regarding the nohup Command
To get all the help regarding the nohup command in Linux, you’ll need to use the
$ nohup --help
Using nohup Command in Linux
Now that you know how the basic syntax of the nohup command looks like, it is time to walk through the varied applications of the command line. From starting single and multiple processes to terminating the running ones, you can do several things seamlessly.
Continuing a Process Using nohup Command
The primary use of the nohup command in Linux is usually keeping a processor command running even when you exit the shell.
$ nohup command
As soon as you run the nohup command in the format mentioned above, all the resources will get added to the nohup.out file available in the parent directory. Once done, the system will continue to execute the process no matter if the shell is exited or not.
When talking about the default scenario, the output of the nohup command is always directed to the nohup.out file. The same is either present on the home directory or the current working destination. Luckily you can alter the same using the redirector operator
> operator followed by the name of the specific file to redirect the nohup output there. For instance, running the following command will save the output to
testfile.sh instead of doing it on
nohup.out. However, you’ll need sudo privileges.
$ sudo nohup ./mn.sh > testfile.sh &
Starting a Single Process in the Background
For starting a process in the background, all you need to do is use the “&” symbol. The symbol instructs the system to run a specific command in the background.
$ nohup command &
The overall process is similar to how the usual nohup command works; the only differentiating factor is that it returns to the shell prompt as soon as the session ends. Using the
-fg flag, however, helps bring the process to the forefront.
Starting Multiple Process in the Background with Linux nohup Command
Similar to how you run a single process in the background, you can employ the nohup command and execute multiple processes simultaneously. Run the command in the following format:
$ nohup bash -c ‘Process1 && Process2 && Process 3’ > output.txt
Terminating the Process Running in the Background
Besides running a process in the background, you can use the nohup command to terminate one. Use the
kill command in the following format:
$ kill -9 PID
The PID or the process identifier is a number that is used by Unix operating system kernels. If you’re wondering how to gain access to the PID of the process you want to terminate, there are two easy ways of getting it done.
Firstly, you can use the nohup command together with the & symbol. Alternatively, the
pgrep -a flag can also be employed to find the PID of any process.
$ pgrep -a ping
Running the code mentioned above will simply list all the PIDs that correspond to the ping command. Note that down and input the same alongside the
$ kill -9 function to terminate the ping process from running in the background.
Other nohup Command Utilities
-p: It modifies the process specified and ignores all hangup signals.
$ nohup ksh filename: Runs file in the Korn shell.
And with that, we’re done talking about the nohup command in Linux. The article tells you how to use nohup commands to run processes in the background and terminate the same at will. Everything about this command-line utility is seamless, provided you’re aware of the appropriate approach.
If this guide helped you, please share it.