What's the difference between Nohup and &? Many students have responded, but not all of the students understand all right, today to dig their own pits to fill out.
The test code is as follows:
is a dead loop program that outputs hello and the number of loops, and sleeps for 1 seconds per output line.
What is the effect of using the./a.out foreground running program?
The program outputs a string at the terminal every second.
At this point, if you type CTRL + C, the program receives a SIGINT signal, and if you do not do special processing, the default behavior of the program is to terminate (for example).
Using ./a.out& background to run the program, what will be the effect?
Such as:
PS Confirm that the process is still running and the process number is 32389.
At this point, if you turn off the session, the program will receive a sighup signal, what happens now?
PS re-confirm, you can see that after the session is closed, the process number is 32389 a.out process is also closed.
What is the effect of using Nohup./a.out?
Using Nohup to run the program a.out, you will find:
Process number does not appear in the foreground
There is a "ignore input, output to nohup.out" hint
The output of Hello also does not appear in the foreground
Manual PS Look at the process number, this time the a.out process number is 32437.
At this time if the session is closed, the program will receive a SIGHUP signal, will the program shut down?
After the session is closed, PS to see again, id 32437 of the a.out process is still in.
These can only be killed by kill the program, after Killall, PS View process has been closed.
After killall, view found a nohup.out file, but the size of this file is 0, a little strange, when the start of the program, obviously prompted the "appending output to Nohup.out" Ah, first left the problem here, test the CTRL +c.
Still, if you use Nohup to start a.out, if you type CTRL + C, the program receives the SIGINT signal and shuts down directly.
Finally test the Nohup and & use at the same time, that is, with Nohup./a.out & Run the program, what will be the effect?
After you run the program using Nohup./a.out &, you can see:
will show the process number at the terminal is 32524
There will also be a "ignore input, output to nohup.out" hint
Type CTRL + C to send a SIGINT signal that does not seem to respond.
Close session, send Sighup signal, and then take a look.
The process with ID 32524 still exists, and it can only be closed with kill.
Conclusion
Run the program using the & background:
The result is output to the terminal
Use CTRL + C to send SIGINT signals, program immunity
Close session send Sighup signal, program close
To run the program using nohup :
Results are output to nohup.out by default
Using CTRL + C to send a SIGINT signal, the program shuts down
Close session send Sighup signal, program immunity
Nohup and & are often used to start programs on weekdays :
At the same time, there is a best practice:
This article is reproduced from the public number: architect of the road, because the old can not find the address, make a record ~
The benefits of Linux Nohup and &