How to use nohup command in Linux?

0/5 No votes

Report this app

Description

When you execute a command in a Linux system it starts a process and when you exit from the terminal it gets automatically terminated. In some situations, you may want to keep running a process even after exiting from the terminal. So how will you do that?

Well, we can use nohup (No Hang Up) command which keeps processes running even after you close your terminal. It prevents the processes from receiving the SIGHUP signal which is the signal sent to a process when its controlling terminal is closed.

In this article, I will discuss the usage of nohup command along with some examples.

Syntax of nohup command in Linux

The syntax of how to use nohup command in Linux is given below.

nohup command [ argument.. ]

Now let’s see some usage of this command.

Usage of nohup command in Linux

The following examples show the usage of nohup command in Linux.

Check the version of nohup command

Use the given command to see the version of nohup installed in your system.

nohup --version

This will display the output as given in the image below.

nohup version

Starting a process using nohup command

When you run a command using nohup command this will start a process that will run even after you exit from the terminal.

For example –

Let’s say we have a sample file called testfile.sh to execute this use –

nohup bash testfile.sh

This will execute the given file and append the output to nohup.out

use nohup command

Now you can see the output which is saved in nohup.out file by using the given command –

cat testfile.sh

testfile output

If you want to redirect the output to a specific file then use –

nohup bash testfile.sh > output.txt

Start a process in the background

You can run a command in the background by appending the & symbol at the end of the command.

For example –

nohup bash testfile.sh &

If you want to bring the process to the foreground then use fg command.

nohup background

You can see help on nohup command by using –

nohup --help

Conclusion

I hope now you understand the usage of nohup command in Linux. Now if you have a query then write us in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *