Use nohup command instructions in python

nohup function

nohup is the abbreviation of no hang up, which means not to hang up.

If you are running a process and you feel that the process will not end when you log out of your account, you can use the nohup command, which can continue to run the corresponding process after you log out of the account/close the terminal.

Example operation

For example, run django project

  1. For example, enter the project directory:

cd ding_month_1

  1. Run the following program:

nohup python ./manage.py runserver 0.0.0.0:8080&

  1. At this point, it is already running.

Precautions:

The standard output of the original program is automatically redirected to the nohup.out file in the current directory, which plays the role of log. After nohup is executed successfully, click the close program button to close the terminal, the session corresponding to the command will be disconnected, and the process corresponding to nohup will be notified of shutdown.

Therefore, after using the nohup command to run the command in the background, you need to use exit to exit the current account normally, so as to ensure that the command is always running in the background.

Supplementary knowledge: Python running in the background-detailed explanation of nohup and 2 &1

1. Code

nohup python my.py /usr/local/python/xxf/my.log 2 &1 &
nohup tomcat.sh /dev/null 2 &1 &

Two. nohup command

Nohup refers to running continuously, which is the abbreviation of no hang up, which means uninterrupted, not hanging up. When running a process, you can use nohup if you don't want it to be closed when you log out of your account.

Nohup is not specified, so the output will go to nohup.out

III. 2 &1

0 Represents stdin standard input, the content entered by the user keyboard

1 Represents stdout standard output, the content output to the display

2 Indicates stderr standard error, error content

2 &1 is a whole, there can be no spaces on the left and right, that is, the error content is redirected and input to the standard output.

4. Overall meaning and &

& Runs in the background

The first code, run the my.py script in a python environment without interruption, and redirect the output of the script into my.log (meaning append, if used, the content will be cleared)

my.py my.log is the same as my.py 1 my.log, except that 1 (standard input) is omitted, and the following my.log 2 &1 input 2 (error content) to standard output, and then the previous standard The output is input to my.log, meaning errors and standard content will be output to my.log, in fact, the code can be split into two pieces.

nohup python my.py 1 /usr/local/python/xxf/my.log &
nohup python my.py 2 /usr/local/python/xxf/my.log &

The above code is to output errors and standards to my.log. The original code is just simplified.

Adding the & in 2 &1 is to distinguish file 1 and 1 (standard output). If it is 2 1, then the error content will be output to file 1.

nohup tomcat.sh /dev/null 2 &1 & is the same, throw all errors and standards into /dev/null and destroy them all.

The above description of using the nohup command in python is all the content shared by the editor. I hope to give you a reference.

Recommended Posts

Use nohup command instructions in python
Use of Pandas in Python development
Use of numpy in Python development
How to use SQLite in Python
How to use and and or in Python
Functions in python
Python crawler-beautifulsoup use
How to use the round function in python
How to use the zip function in Python
How to use dpkg command in Ubuntu system
How to use the format function in python
How to use code running assistant in python
Use command to clear specific hostname in CentOS7
Join function in Python
12. Network Programming in Python3
print statement in python
Installation and use of GDAL in Python under Ubuntu
Use supervisor in ubuntu
Python3 external module use
Concurrent requests in Python
Install python in Ubuntu
Context management in Python
Arithmetic operators in python
Python operation yaml instructions
Write gui in python
MongoDB usage in Python
Str string in Python
Computational Geometry in Python
Concurrent requests in Python (part 2)
Subscripts of tuples in Python
Talking about inheritance in Python
Use of Anaconda in Ubuntu
Noteworthy update points in Python 3.9
What databases can python use
Containerize Python applications in 3 minutes
How to use python tuples
What is introspection in python
What is object-oriented in python
Generators and iterators in Python
Talking about strings in Python