Background:
Some scripts encountered in the project need to run through the background process, guaranteed not to be interrupted, before all through the Nohup, &, screen to achieve, with the ability to do a start/stop/restart/reload service startup idea found in supervisor. About Supervisor's introduction in the online general search summarized as follows:
Supervisor is a set of common process management programs developed in Python that can turn a normal command-line process into a background daemon and monitor the status of the process, which can be restarted automatically when the exception exits. It starts by fork/exec the managed process as a supervisor child process, so that the path to the executable file of the process to be managed is written in the Supervisor configuration file. Also realizes when the child process hangs, the parent process can obtain the information which the child process hangs up accurately, may choose whether oneself starts and the alarm. Supervisor also provides a feature that allows you to set a non-root user for Supervisord or each child process, and this user can manage the corresponding process.
Description
1, installation
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
1:easy_install Installation: Easy_install SUPERVISOR2:PIP installation: Pip install Supervisor3:debian/ubuntu can be installed directly via apt: Apt-get install Supervisor
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
2, configuration file
1) after installing via Apt-get install, the Supervisor configuration file is:
/etc/supervisor/supervisord.conf
The Supervisor configuration file is incomplete by default, but in most cases the basic functionality described above is already satisfied. the child process configuration files that it manages are:
/etc/supervisor/conf.d/*.conf
Then, start writing a sub-process configuration file for the script you need, let supervisor manage it, and put it in the/etc/supervisor/conf.d/directory. With. conf as the extension (each process configuration file can be split separately or associated scripts can be put together). If any, define an option group (/ETC/SUPERVISOR/CONF.D/TEST.CONF) for the project name associated with the script:
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
#项目名 [Program:blog] #脚本目录directory =/opt/bin# script Execution Command command=/usr/bin/python /opt/bin/test.py# Supervisor start when the start, the default trueautostart=true# when the program exit, the programs will not automatically restart, the default unexpected# set the child process automatically restart after the situation, there are three options, False,unexpected and True. If False, no matter what the circumstances, will not be restarted, if unexpected, only if the process's exit code is not defined in the following Exitcodes autorestart=false# This option is how many seconds after the child process started, At this point, if the state is running, we think the boot was successful. The default value is 1startsecs=1# log output stderr_logfile=/tmp/blog_stderr.log stdout_logfile=/tmp/blog_stdout.log # User identity for script run user = zhoujy #把 stderr Redirect to stdout, default falseredirect_stderr = true#stdout log file size, default 50MBstdout_logfile_maxbytes = 20M#stdout Number of log file backups stdout_logfile_backups = 20[program:zhoujy] #说明同上directory =/opt/bin command=/usr/ Bin/python /opt/bin/zhoujy.py autostart=true autorestart=false stderr_logfile=/tmp/zhoujy_ stderr.log stdout_logfile=/tmp/zhoujy_stdout.log #user = zhoujy
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
2) after installation via Easy_install, the configuration file does not exist and needs to be imported by itself.
①: Run echo_supervisord_conf print out a sample of the configuration file, sample description can see Supervisor (a) The detailed description of the basic article, if the sample is set to a configuration file:
1: Run echo_supervisord_conf, view Configuration sample: Echo_supervisord_conf2: Create configuration file: echo_supervisord_conf >/etc/supervisord.conf
②: Configure a child process configuration file that can be directly in the supervisor ;[ Program:theprogramname] set in.
Detailed sub-process configuration file:
Sample:
650) this.width=650; "id=" Code_img_closed_6004f9a0-fc66-4991-a1f7-0cfb16d2ca47 "class=" code_img_closed "src="/img/ Jia.gif "style=" margin:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle; "/> View Code
Description
650) this.width=650; "id=" code_img_closed_d2187784-552f-482f-8b91-1cb74f0b9553 "class=" code_img_closed "src="/img/ Jia.gif "style=" margin:0px;padding:0px 5px 0px 0px;border:0px;vertical-align:middle; "/> View Code
Change to your own actual configuration file: The same as the ① above.
3: Run
1) apt-get Install supervisor can be run directly through /etc/init.d/supervisor:
/etc/init.d/supervisor start
2) Run Supervisord with Easy_install installed supervisor:
Supervisord
4:web interface operation
The [inet_http_server] option group needs to be added to the Supervisor configuration file: The management of the control sub-thread can then be accessed through http://10.211.55.11:9001.
[inet_http_server]port=10.211.55.11:9001username=userpassword=123
Effect:
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/163084/201611/163084-20161118021632920-889244058. PNG "width=" 691 "height=" 187 "style=" margin:0px;padding:0px;border:0px; "/>
5: Child process Management (Supervisorctl)
1) View the status of all child processes:
# supervisorctl Statusblog RUNNING pid 2395, uptime 0:08:41zhoujy RUNNING pid 2396, uptime 0:08:41
2) Turn off and turn on the specified sub-process:
# supervisorctl Stop zhoujyzhoujy:stopped# supervisorctl start zhoujyzhoujy:started
3) Close and open all sub-processes:
# supervisorctl Stop allblog:stoppedzhoujy:stopped# supervisorctl start allblog:startedzhoujy:started
4): Additional parameters: Supervisor Auto-Start (autostart=true) and child process exit after opening (autorestart=ture)
More parameters can be seen in the official documentation and supervisor (a) The description of the basic article.
Brief description of Process management supervisor