Linux uses supervisor to manage processes

Source: Internet
Author: User

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

fo = open (' Xx.txt ', ' W ')

While 1:

Fo.write (' Hello World ')

Print (' Hi ')

Time.sleep (1)

If you use Python hello.py then the console always print Hi, will not be able to do other work, you must xshell open a window. If CTRL +c, the program ends directly.

Make it work in the background can end add a &, but so the Xshell window closed, it is over, you can see if Notepad has been added to prove this. This time will also add a nohup in front, so all commands are nohup python hello.py &

Nohup logs the log of the program, normal print does not go to record, if the log output is not redirected, The default is the current folder under the generation of a nohup.out, the more and more time, to a large file read and write things will be slow, and may be full of disk, check the next need a shell script to engage in this, very troublesome.

And I want to end this hello.py what to do, to go to Ps-ef |grep .... , then kill, to restart again you need to enter Nohup python hello.py & again, the longer the command is more troublesome, of course, can also write the command to the shell script to reduce input, but the management of the process is still a bit of trouble.

Supervisor just used it.

Supervisord-c xxx.conf Start, if the individual supervisor that is the default to use etc/supervisor.conf this configuration

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

Remove the most of the following include comments, each using a separate configuration file, the Supervisorconfig folder has two project profiles, *.ini can contain these two.

[Program:automobile]
Command=/root/miniconda3/bin/uwsgi--ini Uwsgib.ini; Program Start command
Directory=/root/xxxx/automobile
Autostart=true; It starts automatically when the Supervisord is started.
startsecs=10; No exception exits after 10 seconds of startup, which means the process starts normally and defaults to 1 seconds
Autorestart=true; Automatic restart after program exit, optional value: [Unexpected,true,false], default is unexpected, indicating that the process was accidentally killed before restarting
startretries=3; Startup failed auto Retry number, default is 3
User=root; Which user to use to start the process, the default is root
priority=999; Process start priority, default 999, small value priority start
Redirect_stderr=true; REDIRECT stderr to stdout, default false
STDOUT_LOGFILE_MAXBYTES=20MB; StdOut log file size, default 50MB
Stdout_logfile_backups = 20; StdOut log file backup number, default is 10
; StdOut log file, be aware that it does not start properly when the specified directory does not exist, so you need to create the directory manually (Supervisord automatically creates the log file)
Stdout_logfile=/root/yangdefeng/touna/automobile/logs/automobile.out
Loglevel=debug; loglevel Specifies the level of logging, the log output from Python's print statement is not recorded in the log file, and it needs to be paired with a Python logging module to output a specified level of log.
Stopasgroup=true; default is false, whether to send a stop signal to the process group when the process is killed, including child processes
Killasgroup=true; default = False to send a kill signal to the process group, including child processes


Another file, which opened two identical processes this time.
[Program:daili]
Command=/root/miniconda3/envs/py27/bin/python dailiip.py; Program Start command
directory=/root/xxx/Proxy IP Access
numprocs=2; default is 1,; If Numprocs is not 1,process_name, it is important to include process_num in the expression to distinguish between different processes.
process_name=% (program_name) s_% (process_num) 02d
Autostart=false; It starts automatically when the Supervisord is started.
startsecs=10; No exception exits after 10 seconds of startup, which means the process starts normally and defaults to 1 seconds
Autorestart=true; Automatic restart after program exit, optional value: [Unexpected,true,false], default is unexpected, indicating that the process was accidentally killed before restarting
startretries=3; Startup failed auto Retry number, default is 3
User=root; Which user to use to start the process, the default is root
priority=999; Process start priority, default 999, small value priority start
Redirect_stderr=true; REDIRECT stderr to stdout, default false
STDOUT_LOGFILE_MAXBYTES=20MB; StdOut log file size, default 50MB
Stdout_logfile_backups = 20; StdOut log file backup number, default is 10
; StdOut log file, be aware that it does not start properly when the specified directory does not exist, so you need to create the directory manually (Supervisord automatically creates the log file)
stdout_logfile=/root/yangdefeng/touna/Proxy IP Get/logs/daili.out
Loglevel=debug; loglevel Specifies the level of logging, the log output from Python's print statement is not recorded in the log file, and it needs to be paired with a Python logging module to output a specified level of log.
Stopasgroup=false; default is false, whether to send a stop signal to the process group when the process is killed, including child processes
Killasgroup=false; default = False to send a kill signal to the process group, including child processes

For example, I distributed crawler, first open two exactly the same process, of course, can be used in the PY multiprocessing out two processes, you can also run a script two times is not two processes.
Then you can specify numprocs=2,nmprocess greater than 1, be sure to set process_name=% (program_name) s_% (process_num) 02d, the following format name casually, in short, to be able to distinguish it is two well, Otherwise supervisorctl start programe How to know when to start what process. If you do not set the program name to start the Supervisord service will be error.


Use Supervisorctl to enter Supervisor Customer order Management
Start XX
Stop xx to start and stop the program.

Reload update can be used when configuration modifications are required to take effect.



You can also open the Web service to manage the program, in the configuration need to remove the comments;

Enter the Linux address plus port 9001, and you'll see the program status when you log in with your account and password. This above can control clearly know, start those programs, and control program start and stop, click Tail-f can see the log information of the program in the Web, do not go to Xshell to use Tali-f view.

Very handy stuff that can be used to manage Python and non-Python programs. By the way, it is easy to solve the Nohup log size cutting problem.


Linux uses supervisor to manage processes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.