Linux - Python program nohup running in the background

nohup

  • Nohup refers to running continuously, which is an abbreviation of no hang up, which means uninterrupted and does not hang up. When running a process, if you don't want it to close when you log out of your account, you can use nohup.
  • Nohup is not specified, so the output will go to nohup.out.

run python program

nohup python my.py >> my.log 2>&1 &
# 或者
nohup python my.py >> nohup.out 2>&1 &
# 或者
nohup python my.py &  # 这种写法和上面第二种写法等价

2>&1 redirect error content to standard output

0 means stdin standard input, user keyboard input content
1 means stdout standard output, content output to the display
2 means stderr standard error, error content
2>&1 is a whole, > cannot have spaces on the left and right, and the error content is redirected to input to standard output.

& Background process

  • & to run in the background
  • The first code runs the script my.py continuously in the python environment, and redirects the output of the script to my.log (>> means append, if you use >, the content will be cleared)
  • nohup python my.py >> my.log 2>&1 & is the same as nohup python my.py 1 >> my.log 2>&1 &, except that 1 (standard input) is omitted, and the following my.log 2> &1 inputs 2 (error content) to the standard output, and then the previous standard output is input to my.log, which means that both error and standard content will be output to my.log. In fact, the code can be split into two pieces. Right now:
nohup python my.py >> my.log 2>&1 &
# 等价于下面两行的内容
nohup python my.py 1>> my.log &
nohup python my.py 2>> my.log &
  • The above code is to output both error and standard to my.log, the original code is just simplified.
    The & in 2>&1 is added to distinguish between file 1 and 1 (standard output). If it is 2>1, then it will output the error content to file 1.

potential problem

The nohup.out file is generated, but the content is always empty.

Cause and solution

The output of python is buffered again, causing nohup.out to see the output immediately.
The -u parameter makes python not enable buffering.

nohup python -u my.py >> nohup.out 2>&1 &

I just reprinted the part I used, and the source of the original text is below. I will add some of my own problems below.
Copyright statement : This article is an original article by CSDN blogger "Xiaomeng Tec", following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement for reprinting.
Original link : https://blog.csdn.net/m0_38024592/article/details/103336210

After executing the above command

After execution, there is always a blank line, just like running the program in the foreground, and no command line input prompt pops up. At this time, pressing Enter or CTRL+C will terminate the program. At this time, you only need to directly enter the command you want to use. For example: enter jobs to view the tasks executed in the background of the current terminal (the difference between ps and jobs is that jobs can only view the tasks executed in the background of the current terminal, and they will not be visible if the terminal is changed. The ps command is suitable for viewing the dynamics of instantaneous processes, you can See tasks for other terminals.)

View the tasks running in the background of the current terminal

jobs -l选项可显示当前终端所有任务的PID,jobs的状态可以是running,stopped,Terminated。+ 号表示当前任务,- 号表示后一个任务。

View all current processes

ps aux   #a:显示所有程序  u:以用户为主的格式来显示   x:显示所有程序,不以终端机来区分

# 可以加管道符
ps -aux | grep "test.sh"

Close the command currently running in the background

kill命令:结束进程
1)通过jobs命令查看jobnum,然后执行   kill %jobnum (%jobnum 表示此处用job的id填充,不需要输入%
2)通过ps命令查看进程号PID,然后执行  kill %PID

   如果是前台进程的话,直接执行 Ctrl+c 就可以终止了

Switching and control of front and back processes

1)fg命令

   功能:将后台中的命令调至前台继续运行

   如果后台中有多个命令,可以先用jobs查看jobnun,然后用 fg %jobnum 将选中的命令调出。
2)Ctrl + z 命令

   功能:将一个正在前台执行的命令放到后台,并且处于暂停状态
3)bg命令

   功能:将一个在后台暂停的命令,变成在后台继续执行

   如果后台中有多个命令,可以先用jobs查看jobnum,然后用 bg %jobnum 将选中的命令调出继续执行。

Click to view: process suspension and recovery, ps view process and its status meaning

The difference between nohup and bg

nohup: The feature is that the shell window can be closed, but there is no pause function, use ps to view, kill to kill
ctrl+z hang or bg: the feature is that the shell window cannot be closed, it has a pause function, use jobs to view, close the shell window or kill come to kill.

ctrl+Z 将现在前景的、运行的程序,变成背景的、暂停的工作。
jobs   查看背景中的工作编号。 jobs -l可以查看工作进程编号。
fg %1  调回前景环境运行。将方括号编号为[1]的背景的、暂停的程序,变成前景的、运行的程序。
bg %2  将背景环境中暂停的工作运行起来。将方括号编号为[2]的背景的、暂停的程序,变成背景的、运行的程序。

the difference:

  1. Running in the background (nohup) is not the same as running in the background (bg). The running program in the background (bg) cannot be terminated by ctrl+C, but can be terminated by closing the shell terminal or killing. The program running in the background (nohup) cannot be terminated even when the shell is closed. It needs to be terminated by kill.
  2. Programs in the background environment depend on the current shell window to survive. Processes in the background environment are called jobs. The jobs in the background environment in a shell can be viewed with jobs. When the current shell window is closed, the jobs in the background are also closed. So in other shell windows, jobs cannot find jobs in the background environment.

Rookie notes, personal notes only

Supongo que te gusta

Origin blog.csdn.net/DreamingBetter/article/details/123918531
Recomendado
Clasificación