Javatpoint Logo
Javatpoint Logo

Nohup Command

The nohup stands for no hang-up, it is a Linux utility that keeps the processes running even after exiting the terminal or shell. It prevents the processes from getting the SIGHUP signals (Signal hang up); these signals are sent to the process to terminate or end a process.

Usually, when we exit from the terminal, all the running processes are terminated by default. However, it shows a warning for the running process. If a process takes more time than expected and we need to complete it. In such cases, a nohup command is a handy tool for keep running the process in the background. The best usage of nohup commands is in memory check, synchronization, restarting a server, and more. It is also advantageous when we run a program over ssh.

In the case of ssh connection, if a connection is interrupted, all the running processes are terminated, and we may lose our data. The nohup command overcomes this problem; it ignores all hangup signals and allows the process to continue.

Syntax:

The nohup command can be used in two ways:

or

Let's understand different usage of nohup command with examples:

Checking the nohup version

It is a default command-line utility of a Linux system. Therefore, we don't need to install it. We can check the installed version by executing the following command:

The above command will display the installed version. Consider the below output:

Nohup Command

How to start a process using nohup

If you want to run a process after exiting the terminal, execute the nohup command followed by the process. It is a straight forward process. The process will continue running and will not get killed.

Let's take an example to understand how nohup works. We have a file 'hello.sh,' we will open it using nohup command. Execute the below command:

Now, the cat command will execute completely even if we close the terminal. Consider the below output:

Nohup Command

We can see from the above output, it will create a file 'nohup.out' and append the output to it.

To display the output, execute the below command:

Consider the below output:

Nohup Command

Moreover, we can also redirect the output to a specified file. To do so, execute the command as follows:

The above command will redirect the output to a specified file 'newfile.' To verify the output, execute the command as follows:

Consider the below output:

Nohup Command

Starting a background process

To start a process in the background, use the '&' symbol after the command. It will execute our process in the background. For example, if we want to ping javatpoint.com, execute the command as follows:

The above command will ping us with javatpoint.com and redirect the process to the background.To check the process, execute the pgrep command as follows:

Consider the below output:

Nohup Command

To kill the process, execute the kill command with the given PID.

The above command will kill the background processes. Consider the below output:

Nohup Command
Next TopicLinux at command




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA