Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of the usage and difference of nohup and & in Linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Example:

Let's use the python code loop_hello.py as an example. The code is used to loop out the number of cycles and hello worlds, sleep 1 second after each output.

The sample code is as follows:

Import time def main (): I = 0 while True: I + = 1 print ('% d: hello worldview'% (I)) time.sleep (1) if'_ _ main__' = = _ _ name__: main ()

Run loop_hello.py and the output is as follows:

The program will output a string at the terminal every second. If you type Ctrl+C, the program will receive a SIGINT signal. If no special processing is done, the default behavior of the program is to terminate (as shown in the figure above).

&

Using python loop_hello.py &, the effect is as follows:

First of all, the process number is 2367 displayed on the terminal.

Type Ctrl + C, send out the SIGINT signal, and the program will continue to run

If you turn off session, the program will receive a SIGHUP signal, which can be seen through ps aux | grep loop_hello.py. Process 2367 is also closed.

Nohup

Using nohup python loop_hello.py, the results are as follows:

The process number does not appear at the front desk, and there is a prompt to "ignore input and append output to" nohup.out ". The output of hello does not appear in the foreground.

If you turn off session, will the program shut down?

Use ps aux | grep loop_hello to check the process number to turn off session, the program will receive a SIGHUP signal to use ps aux again | grep loop_hello, it is found that there are still kill missing processes in the process.

Test Ctrl + C

Use nohup to start loop_hello.py. If you type Ctrl+C, the program closes directly after receiving the SIGINT signal.

& used in conjunction with nohup

Use nohup python loop_hello.py & to run the program, the results are as follows:

Type Ctrl + C, send SIGINT signal and use ps aux to view, the process still exists.

Close session, send SIGHUP signal and use ps aux to view, the process still exists.

If you want to terminate the process, you can only use kill

Summary:

Use & run the program in the background:

The result will be output to the terminal to send SIGINT signal using Ctrl + C, the program immune shuts down session to send SIGHUP signal, and the program closes.

Run the program using nohup:

The result will be output to nohup.out by default and use Ctrl + C to send SIGINT signal. The program closes and closes session to send SIGHUP signal. The program is immune.

Nohup and & are often used online to start programs:

Simultaneously immunize SIGINT and SIGHUP signals

Well, the above is the whole content of this article. I hope the content of this article has a certain reference and learning value for your study or work. Thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report