How To Use Linux Nohup Command To Execute Commands After Exit Shell or Terminal?


nohup is a command used to ignore the HUP signal. HUP signal is used to kill a shell command if the parent shell exits. Think that we logged in with ssh and run a command. But the command execution takes a lot of time. We need to log out. In normal sense, if we log out our command execution will end. In this case, we will provide nohup command to prevent command termination even the ssh logout. Nohup is natively provided by Linux distributions, Unix and BSD.

Syntax

As we can see nohup command has two types of syntax. In the first one, we will provide the command and arguments. In the second one, we can use just two options named --help and --version .

nohup COMMAND [ARG]... 
nohup OPTION

Usage

Nohup is a very easy command to use in the previous syntax section as wee see. We will just provide command after nohup command we want to execute.

$ nohup top &
Nohup Usage

We have appended an ampersand & to make run our process in the background and not bind to the current terminal.

Execute with Redirection

We can redirect nohup  command output to the different commands or files. As bash provides > for redirection, we will use it to write nohup  command a  file named myoutput.txt in this example.

$ nohup bash loop1.sh > myoutput.txt &

Use with Find Command

In this example, we will use find command which will run even we will close the current shell.

$ nohup find -size +1000k > big_files.txt &

We also use & which sends the process background and gives the current shell to us. This will give us the message ignoring the input and redirecting stderr to stdout like below.

LEARN MORE  What Does "ps aux" Command Mean and Do In Linux
Find Nohup Example
Find Nohup Example

Situations Of Nohup Usage

We can use nohup command in the following cases.

  • Synchronization
  • Tomcat Server Restart
  • Memory Check

Leave a Comment