The benefits of Linux Nohup and &

Source: Internet
Author: User
Tags sigint signal

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:

    • First the terminal will show the process number is 32389

    • Type CTRL + C to emit a SIGINT signal and the program will continue to run

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 :

    • Simultaneous immune SIGINT and SIGHUP signals

At the same time, there is a best practice:

    • Do not export information to terminal standard output, standard error output, and log component to log information

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 &

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.