What is the difference between the following methods of launching background jobs?
./long_job.sh > logfile &
nohup ./long_job.sh > logfile &
(by default, option 2 this will print to nohup.out
or you can redirect to a filename of your choice).
What are the advantages of nohup
? I tested the following shell script as a proxy for long_job.sh
and exited the terminal (with ctrl-D
)
while [ 1==1 ]; do
jot -r -c 4 A Z | rs -g 0 4
echo $$ # Echo pid
sleep 2
done
There is a nohup question with similar title, but it asks about difference between redirection of stdout and stderr
EDIT: This question does not talk about explicit file redirect. Without redirect, when shell quits, the child process dies because it does not have stdout to print to. But with redirect it does not need the shell window to be open. Operationally, both approaches above achieve the same outcome (which is run the job even after terminal closes). My question about was the difference between the two approaches.