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

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

Example:

We use the Python code loop_hello.py as an example. The code outputs the number of loops and hello world! in a loop, and sleeps for 1 second after each output.

The sample code is as follows:

import time
 
def main():
 i = 0 
 while True:
  i += 1
  print('%d: hello world!' %(i))
  time.sleep(1)
 
if '__main__' == __name__:
 main()

Run loop_hello.py and the output is as follows:

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

&

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

First, the process number will be displayed in the terminal as 2367

Type Ctrl + C to send a SIGINT signal and the program will continue to run.

When the session is closed, the program will receive a SIGHUP signal. You can see through ps aux | grep loop_hello.py that process 2367 is also closed.

nohup

Use nohup python loop_hello.py, the effect is as follows:

  1. No process number appears in the foreground
  2. There is a prompt "Ignore input and append output to "nohup.out""
  3. The output of hello does not appear in the foreground

If I close the session, will the program close?

  1. Use ps aux | grep loop_hello to view the process ID
  2. When you close the session, the program will receive a SIGHUP signal.
  3. Use ps aux | grep loop_hello again and find that the process still exists
  4. Kill the process

Test Ctrl + C

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

& and nohup are used together

Use nohup python loop_hello.py & to run the program. The effect is as follows:

Type Ctrl + C to send a SIGINT signal. Use ps aux to check that the process still exists.

Close the session, send a SIGHUP signal and use ps aux to check that the process still exists

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

Summarize:

Use & to run the program in the background:

  1. The results will be output to the terminal
  2. Use Ctrl+C to send SIGINT signal, program immunity
  3. Close the session and send a SIGHUP signal, the program will close

Run the program using nohup:

  1. The results will be output to nohup.out by default
  2. Use Ctrl+C to send a SIGINT signal and the program will close
  3. Close the session and send SIGHUP signal, the program is immune

On weekdays, nohup and & are often used together to start programs online:

  1. Immune to both SIGINT and SIGHUP signals

Well, that’s all for this article. I hope the content of this article will be of certain reference value to your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • PHP daemon process plus Linux command nohup to implement task execution once per second
  • Linux nohup and tail-f usage
  • Solve the problem of python nohup linux background running output
  • Detailed explanation of the solution to the problem of nohup log output being too large under Linux
  • Linux nohup to run programs in the background and view them (nohup and &)
  • How to use & and nohup in the background of Linux
  • A brief analysis of the examples and differences of using nohup and screen to run background tasks in Linux
  • Linux &, use of nohup and Systemctl

<<:  Ubuntu 18.04 installs mysql 5.7.23

>>:  Tutorial on installing mysql5.7.23 on Ubuntu 18.04

Recommend

Working principle and example analysis of Linux NFS mechanism

What is NFS? network file system A method or mech...

Create a movable stack widget function using flutter

This post focuses on a super secret Flutter proje...

A guide to writing flexible, stable, high-quality HTML and CSS code standards

The Golden Rule Always follow the same set of cod...

MySQL uses covering index to avoid table return and optimize query

Preface Before talking about covering index, we m...

React implements the sample code of Radio component

This article aims to use the clearest structure t...

Summary of some common methods of JavaScript array

Table of contents 1. How to create an array in Ja...

Node implements search box for fuzzy query

This article example shares the specific code for...

Implementation of multi-port mapping of nginx reverse proxy

Code Explanation 1.1 http:www.baidu.test.com defa...

Detailed explanation of MySQL's FreeList mechanism

1. Introduction After MySQL is started, BufferPoo...

Summary of common Mysql DDL operations

Library Management Create a library create databa...

Detailed process of implementing the 2048 mini game in WeChat applet

Rendering Example Code Today we are going to use ...

Using radial gradient in CSS to achieve card effect

A few days ago, a colleague received a points mal...