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
cd ding_month_1
nohup python ./manage.py runserver 0.0.0.0:8080&
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