Nohup and &

& : runs in the background, but when the user exits (suspends), the command automatically exits. This means that when you execute Python test.py &, even if you press CTRL + C, Python test.py will still run (because it is immune to SIGINT signals). Note, however, that if you close the shell directly, the Python process also disappears. As you can see, the & background is not hard (because it is not immune to SIGHUP signals).

Nohup: running without hanging up. Nohup means ignoring SIGHUP signals, so when nohup Python test.py is run, close the shell and the Python process is still alive (immune to SIGHUP signals). Note, however, that if you use Ctrl + C directly in the shell, the Python process will also disappear (because it is not immune to SIGINT signals).

Note that nohup does not have the function of background running, that is, running a command with nohup can make the command run permanently, which has nothing to do with the user terminal. For example, if we disconnect the SSH connection, its operation will not be affected. Note that nohup does not mean background running. & is the background run

The code for

Start with a Python test code that prints HelloWorld and numbers in an infinite loop, waiting 1 second for each line:

#! /usr/bin/python # -* -coding: utf-8 -* -import time var = 1 while var > 0: ", var var = var +1 time.sleep(1Copy the code

The front desk operation

python test.py
Copy the code

The program outputs a string at the terminal every second.

If you type Ctrl+C at this point, the program receives a SIGINT signal, and the default behavior of the program is to terminate if no special treatment is done (see figure above).

The background

python test.py &
Copy the code

As shown in the figure above, the process number 8778 is first displayed on the terminal

If YOU type Ctrl + C to signal SIGINT, will the program continue?

As shown in the image below, typing Ctrl + C will still continue:

Perform ps – ef | grep test. Py query, process exists, as shown in the figure below:

If session is closed, xshell is closed, and the application receives a SIGHUP signal, what happens?

Re-open xshell session:

As shown above, the program will not continue to output, and perform ps – ef | grep test. Py just the process of open and closed

Nohup run

nohup python test.py
Copy the code

Run the test.py program with nohup, as shown above:

  • The process number does not appear in the foreground
  • There is a prompt to “ignore input, output to nohup.out”
  • The output of Hello also doesn’t appear in the foreground

As shown above, in another xshell session window to execute ps – ef | grep test. Py, will find that the script has been in operation, the process number is 20085

The previous figure prompted the output to nohup.out, so let’s see what’s inside in a new window

vim nohup.out
Copy the code

As shown in the figure above, the script logs are output in this file.

At this point, if we close the xshell session where the script was executed, the program will receive a SIGHUP signal. Will the program shut down?

As shown above, we in the newly opened xshell create ps – ef | grep test. Py, found that the python process of PID is 20085 still exist.

The process can only be killed by using the kill command:

kill -9 20085
Copy the code

Then, photoshop again to see that the process has been killed.

Use nohup again to execute the Python script: nohup python test.py

Then type Ctrl + C, and execute the ps – ef | grep test. Py check process:

As shown in the figure above, after pressing Ctrl+C, the program receives the SIGINT signal and the process shuts down.

The background does not hang up

Nohup and & are used together. Let’s see what happens:

Run the program with the following instructions:

nohup python test.py &
Copy the code

After running the program with nohup Python test.py &, you can see:

  • The process number 21503 is displayed on the terminal
  • There is also a prompt to “ignore input and output to nohup.out”
  • Type Ctrl + C, send SIGINT signal, nothing seems to happen.

Close the session, send the SIGHUP signal, open the Session window and photoshop:

As shown above, the process with ID 21503 still exists, and you can only kill it later.

conclusion

Using & background run program:

  • The result is output to the terminal
  • Use Ctrl + C to send SIGINT signal, program immune
  • Closing session Sends SIGHUP signal and the program closes

Run the program with nohup:

  • The result is printed to nohup.out by default
  • Use Ctrl + C to send SIGINT signal and the program closes
  • Shut down session and send SIGHUP signal to make the program immune

Use nohup and & to start the program:

  • Both SIGINT and SIGHUP signals are immune

Best practices:

Do not output information to terminal standard output, standard error output, but use the logging component to log information

The nohup command can be used to enter logs into a file

  • If the output file is not specified, the output file is in the current directory by defaultnohup.outfile
  • If the nohup.out file in the current directory is not writable, the output is redirected to the $HOME/nohup.out file.

Here’s an example:

nohup python test.py > test.log 2>&1 &

nohup ping www.baidu.com > ping.log 2>&1 &
Copy the code

Reprint articles: www.cnblogs.com/mingyue5826…