There can be many times where you are suppose to run some application or process in Linux but you don not want it to be terminated by logoff and session disconnection from putty.
nohup command
`nohup' runs the given COMMAND with hangup signals ignored, so that the command can continue running in the background after you log out.
Syntax
# nohup COMMAND [ARG]...
If standard input is a terminal, it is redirected from `/dev/null' so that terminal sessions do not mistakenly consider the terminal to be used by the command.
If standard output is a terminal, the command's standard output is appended to the file `nohup.out'; if that cannot be written to, it is appended to the file `$HOME/nohup.out'; and if that cannot be written to, the command is not run. Any `nohup.out' or `$HOME/nohup.out' file created by `nohup' is made readable and writable only to the user, regardless of the current umask settings.
If standard error is a terminal, it is normally redirected to the same file descriptor as the (possibly-redirected) standard output. However, if standard output is closed, standard error terminal output is instead appended to the file `nohup.out' or `$HOME/nohup.out' as above.
For Example:
In this case the output is appended to ~/nohup.out file
# nohup less /var/log/messages
nohup: ignoring input and appending output to `nohup.out'
NOTE: nohup does not automatically put the command it runs in the background; you must do that explicitly, by ending the command line with an `&'.
You can supress this message and restrict the command from producing or saving any output to nohup.out using the below commands and redirection
# nohup seq 1223434 > file.txt </dev/null &>/dev/null &
[1] 3185
# OR
# nohup seq 1234568 > file.txt 2>&1 &
[1] 3184
# OR
# nohup seq 1234568 > file.txt 2>/dev/null &
[1] 3192
Stadard in, out and err
There are three standard sources of input and output for a program. The program usually prints to standard output, and sometimes prints to standard error. These three file descriptors (you can think of them as “data pipes”) are often called STDIN, STDOUT, and STDERR.
Sometimes they’re not named, they’re numbered! The built-in numberings for them are 0, 1, and 2 for STDIN, STDOUT and STDERR respectively.
disown command
You can also use this command to run any process without any disruption of active terminal or session
If run without any options disown removes the JobID from the list of active jobs. As for now lets see how it can run any process in the background even after the terminal is closed or logged off.
Run a process in background
# seq 1223434232 > file.txt &
[1] 3879
# disown -h 3879
# top
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3879 root 20 0 4084 564 512 R 99.0 0.1 0:23.00 seq
After this I disconnected my complete session and re-logged in to verify if my process is still running
# top
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3879 root 20 0 4084 564 512 R 99.9 0.1 1:01.11 seq
And yes its still in the running status.
If you use disown command without any switch it will remove all the stopped jobs.
For Example
If any of your running process which is stopped by using (Ctrl + Z) can be seen by using
# jobs
[1] Running seq 122354332 > file.txt &
[2]- Running seq 17653434232 > file.txt &
[3]+ Stopped seq 127564232 > file.txt
Run the disown command without any argument by default it will delete the last stopped processes which in our case is (3)
# disown
-bash: warning: deleting stopped job 3 with process group 3896
In case you want to delete a particular stopped job
# disown %3
-bash: warning: deleting stopped job 3 with process group 3904
To delete all the stopped jobs
# disown -a
-bash: warning: deleting stopped job 1 with process group 3915
-bash: warning: deleting stopped job 2 with process group 3916
-bash: warning: deleting stopped job 3 with process group 3917
-bash: warning: deleting stopped job 4 with process group 3918
Related Articles:
10 examples to help you understand top command usage in Unix/Linux
How to run a process in background or bring to foreground in Linux
How to run any script or cron job in Linux for every second