Linux uses supervisor to manage processes

Now suppose a script is, hello.py, the content is

fo = open(‘xx.txt’,’w’)

while 1:

fo.write(‘hello world’)

print(‘hi’)

time. sleep(1)

If you use python hello.py, the console will always print hi, and you will not be able to do other tasks. You must open a window in xshell. If ctrl + c, the program ends directly.

To make it work in the background, you can add an & amp; at the end, but if you close the xshell window in this way, it will be over. You can see if there are any new additions in Notepad to prove this. At this time, nohup will be added in front, so the whole command is nohup python hello.py & amp;, so it will work.

nohup will record the log of the program, and ordinary print will not record it. If the log output is not redirected, a nohup.out will be generated under the current folder by default. This will grow larger and larger with time, and it will be read into a large file. Writing things is bound to be slow, and the disk may be full. After checking, a shell script is needed to do this, which is very troublesome.

And what if I want to end this hello.py, I need to ps -ef |grep. . . . , and then kill. To restart, you need to enter nohup python hello.py & amp; again. The longer the command, the more troublesome it is. Of course, you can also write the command into a shell script to reduce input, but it is still a bit troublesome to manage the process.

The supervisor is just used,

supervisord -c xxx.conf starts, if there is a separate supervisor, the configuration of etc/supervisor.conf is used by default

Add program configuration in etc/supervisor.conf, but this is not very good, the configuration file will become longer and longer,

Cancel the include comment at the bottom, and use a separate configuration file for each project. There are two project configuration files under the supervisorconfig folder, and *.ini can contain these two.

[program:automobile]<br>command=/root/miniconda3/bin/uwsgi --ini uwsgib.ini ; Program startup command<br>directory=/root/xxxx/automobile<br>autostart=True ; Automatically start when supervisord starts<br>startssecs=10 ; If there is no abnormal exit after 10 seconds of startup, it means that the process has started normally, and the default is 1 second<br>autorestart=true ; The program restarts automatically after exiting, optional values: [unexpected, true, false], the default is unexpected, which means that the process restarts after it is accidentally killed<br>startretries=3 ; The number of automatic retries on startup failure, the default is 3<br>user=root ; Which user to use to start the process, the default is root<br>priority=999 ; Process startup priority, the default is 999, and the smaller value starts first<br>redirect_stderr=true ; redirect stderr to stdout, default false<br>stdout_logfile_maxbytes=20MB ; stdout log file size, default 50MB<br>stdout_logfile_backups = 20 ; stdout log file backup number, the default is 10<br>; stdout log file, you need to pay attention that when the specified directory does not exist, it cannot be started normally, so you need to manually create the directory (supervisord will automatically create log files)<br>stdout_logfile=/root/yangdefeng/touna/automobile/logs/automobile.out<br>loglevel=debug ;loglevel specifies the log level, and the log output by Python's print statement will not be recorded in the log file. It needs to be used with Python's logging module to output the log with the specified level.<br>stopasgroup=true ; The default is false, when the process is killed, whether to send a stop signal to this process group, including child processes<br>killasgroup=true; the default is false, send a kill signal to the process group, including child processes<br><br><br>Another file, this time open two identical processes. 
[program:daili]<br>command=/root/miniconda3/envs/py27/bin/python dailiip.py ; program startup command<br>directory=/root/xxx/proxy ip acquisition<br>numprocs=2 ; The default is 1, ; If numprocs is not 1, the expression of process_name must include process_num to distinguish different processes<br>process_name=%(program_name)s_%(process_num)02d<br>autostart=false ; Automatically start when supervisord starts<br>startssecs=10 ; If there is no abnormal exit after 10 seconds of startup, it means that the process has started normally, and the default is 1 second<br>autorestart=true ; The program restarts automatically after exiting, optional values: [unexpected, true, false], the default is unexpected, which means that the process restarts after it is accidentally killed<br>startretries=3 ; The number of automatic retries on startup failure, the default is 3<br>user=root ; Which user to use to start the process, the default is root<br>priority=999 ; Process startup priority, the default is 999, and the smaller value starts first<br>redirect_stderr=true ; redirect stderr to stdout, default false<br>stdout_logfile_maxbytes=20MB ; stdout log file size, default 50MB<br>stdout_logfile_backups = 20 ; stdout log file backup number, the default is 10<br>; stdout log file, you need to pay attention that when the specified directory does not exist, it cannot be started normally, so you need to manually create the directory (supervisord will automatically create log files)<br>stdout_logfile=/root/yangdefeng/touna/ proxy ip acquisition /logs/daili.out<br>loglevel=debug ;loglevel specifies the log level, and the log output by Python's print statement will not be recorded in the log file. It needs to be used with Python's logging module to output the log with the specified level.<br>stopasgroup=false ; The default is false, when the process is killed, whether to send a stop signal to this process group, including child processes<br>killasgroup=false ;The default is false, send a kill signal to the process group, including child processes
<br>For example, my distributed crawler starts two identical processes first. Of course, I can use multiprocessing in py to create two processes, or I can run the script twice, isn't it just two processes?<br>Then you can specify numprocs=2, when nmprocess is greater than 1, you must set process_name=%(program_name)s_%(process_num)02d, the following format name is random, in short, you must be able to distinguish two, otherwise supervisorctl start program How do you know what process to start. If you do not set the program name to start the supervisord service, an error will be reported.<br><br><br>Use supervisorctl to enter supervisor customer order management<br>start xx<br>stop xx to start and stop the program.<br><br>reload update can be used when configuration changes need to take effect.<br><br><br><br>You can also open the web service to manage the program, and you need to remove the comment in the configuration;<br><br>

Enter the address of linux plus port 9001, and log in with the account and password to see the status of the program. You can control and know clearly on this, start those programs, and control the start and stop of the program, click tail -f to see the log information of the program in the web, no need to go to xshell to use tali -f to view it.


A very handy thing that can be used to manage python and non-python programs. By the way, it easily solves the log size cutting problem of nohup. 
<br><br>