使用Supervisor来管理进程
Supervisor是什么
Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems.
Supervisor是用Python实现的一款实用的进程管理工具。Supervisor会帮你把管理的应用程序转成daemon程序,而且可以方便的通过命令开启、关闭、重启进程等操作,而且它管理的进程一旦崩溃会自动重启,这样就可以保证程序执行中断后的情况下有自我修复的功能。
安装与配置
Supervisor本质上是个python包,安装很简易: sudo pip install supervisor
配置文件
运行 echo_supervisord_conf
,会输出一个模板,修修改改就可用了。
将其写入文件: sudo echo_supervisord_conf > supervisord.conf
如果没有root权限,可以将配置文件写在本地,之后用 -c
参数来指定配置文件: supervisord -c supervisord.conf
。推荐做法是将配置文件写到 /etc/supervisord.conf
生成默认配置文件(推荐做法)
echo_supervisord_conf > /etc/supervisord.conf mkdir /etc/supervisord.conf.d
每个需要管理的进程分别写在一个文件里面,放在/etc/supervisord.conf.d/目录下,便于管理。修改配置文件,include区段修改为
[include] files = /etc/supervisord.conf.d/*.conf
如需要访问web控制界面,inet_http_server区段修改为
[inet_http_server] port=0.0.0.0:9001 username=username password=password
将supervisord加入系统服务,文件: /etc/init.d/supervisord
使用
- Supervisord安装完成后有两个可用的命令行
supervisor
和supervisorctl
- supervisord:初始启动Supervisord,启动、管理配置中设置的进程:
启动supervisord:
supervisord -c /etc/supervisord.conf
- supervisorctl stop programxxx,停止某一个进程(programxxx),programxxx为[program:programxxx]里配置的值。stop可替换为start/restart/reload
edX中(Cypress版本)
Shutdown the lms and cms:
sudo /edx/bin/supervisorctl > stop edxapp: edxapp_worker:
See what services are running
sudo /edx/bin/supervisorctl status
Restarting Services
LMS/CMS : sudo /edx/bin/supervisorctl restart edxapp:
Workers : sudo /edx/bin/supervisorctl restart edxapp_worker:
stop everything except the LMS and CMS
sudo /edx/bin/supervisorctl stop discern discern_celery
forum ora ora_celery xqueue xqueue_consumer xserver edxapp_worker:
edx中相关配置文件
搜索 sudo find / -name *supervisord*
。观察发现lms和cms相关的配置文件在 /edx/app/supervisor/supervisord.conf
, /edx/app/supervisor/conf.d/*.conf
其中lms的配置文件为
[program:lms] command=/edx/app/edxapp/venvs/edxapp/bin/gunicorn -c /edx/app/edxapp/lms_gunicorn.py lms.wsgi user=www-data directory=/edx/app/edxapp/edx-platform environment=PORT=8000,ADDRESS=127.0.0.1,LANG=en_US.UTF-8,DJANGO_SETTINGS_MODULE=lms.envs.aws,SERVICE_VARIANT="lms",PATH="/edx/app/edxapp/venvs/edxapp/bin:/edx/app/edxapp/edx-plat form/bin:/edx/app/edxapp/.rbenv/bin:/edx/app/edxapp/.rbenv/shims:/edx/app/edxapp/.gem/bin:/edx/app/edxapp/edx-platform/node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" stdout_logfile=/edx/var/log/supervisor/%(program_name)s-stdout.log stderr_logfile=/edx/var/log/supervisor/%(program_name)s-stderr.log killasgroup=true stopasgroup=true