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

MySQL prepare principle detailed explanation

Benefits of Prepare The reason why Prepare SQL is...

Using Openlayer in Vue to realize loading animation effect

Note: You cannot use scoped animations! ! ! ! via...

Docker Compose one-click ELK deployment method implementation

Install Filebeat has completely replaced Logstash...

Introduction to MySQL role functions

Table of contents Preface: 1. Introduction to rol...

JavaScript to achieve click image flip effect

I was recently working on a project about face co...

About MYSQL, you need to know the data types and operation tables

Data Types and Operations Data Table 1.1 MySQL ty...

JavaScript common statements loop, judgment, string to number

Table of contents 1. switch 2. While Loop 3. Do/W...

How to reset your Linux password if lost

1. The startup menu is to move the cursor to the ...

My CSS framework - base.css (reset browser default style)

Copy code The code is as follows: @charset "...

How to install Windows Server 2008 R2 on Dell R720 server

Note: All pictures in this article are collected ...

CSS -webkit-box-orient: vertical property lost after compilation

1. Cause The requirement is to display two lines,...

Tutorial on how to install and use Ceph distributed software under Linux

Table of contents Preface 1. Basic Environment 1....

Vue implements button switching picture

This article example shares the specific code of ...