How To Use Linux Nohup Command To Execute Commands After Exit Shell or Terminal?
nohup is a command used to ignore 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.
As we can see
nohup command have two type of syntax. In the first one we will provide the command and arguments. In the second one we can use just two options named
nohup COMMAND [ARG]...
Nohup is very easy command to use in previous syntax section as wee see. We will just provide command after nohup command we want to execute.
$ nohup top &
We have appended 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 current shell.
$ nohup find -size +1000k > big_files.txt &
We also use
& which sends the process background and gives current shell to us. This will gives us the message ignoring input and redirecting stderr to stdout like below.
Situations Of Nohup Usage
We can use nohup command in the following cases.
- Tomcat Server Restart
- Memory Check