写在前面
今天想使用supevisor管理Linux中的进程,主要是uwsgi,因为我现在的服务器上有三个web应用同时在运行,其中一个是wordpress,由nginx独立运行,另外两个采用nginx+uwsgi的方式,这样如果对其中一个作出修改,难免会连带着停掉另外一个的服务,及其不方便,所以想到用supervisor来管理,意外的解决了困扰我很久的缓存问题。
配置环境
安装与启动:
#安装服务
sudo apt install supervisor -y
查看状态:
sudo service supervisor status
修改配置文件:
sudo vim /etc/supervisor/supervisor.conf
#打开末尾两行的注释
[include]
files = /etc/supervisor/conf.d/*.conf
添加个人配置:
#创建文件
sudo vim /etc/supervisor/conf.d/myconf.conf
贴入以下内容:
;程序名称,在 supervisorctl 中通过这个值来对程序进行一系列的操作
[program:DjangoBlog]
;这是uwsgi的命令,作为参考,分号用于注释该行
;command =/usr/local/bin/uwsgi /home/server/python/DjangoBlog_uwsgi/DjangoBlog.ini
;这条用于启动gunicorn脚本
command = /home/server/python/gunicorn/DjangoBlog.sh
;在 supervisord 启动的时候也自动启动
autostart = true
;进程停止信号
stopsignal=QUIT
;选择执行的用户
user=server
;五秒无异常则认为启动成功
startsecs = 5
;异常退出时的重启次数
startretries = 3
;异常退出自动重启
autorestart = true
;把 stderr 重定向到 stdout,默认 false
redirect_stderr = true
;日志文件最大值
stdout_logfile_maxbytes = 20MB
stdout_logfile = /var/log/DjangoBlog_stdout.log
stderr_logfile = /var/log/DjangoBlog_err.log
在使用supervisor时需要注释掉uwsgi和gunicorn的deamon选项。
其他关于gunicorn和uwsgi、nginx的配置本文不再叙述
管理进程
#更新配置
sudo supervisorctl update
#重启,所有进程会一并重启
sudo supervisorctl reload
#查看进程运行状态
sudo supervisorctl status
#关闭某个服务
sudo supervisorctl stop DjangoBlog
#启动某个服务
sudo supervisorctl start DjangoBlog
#重启某个服务
sudo supervisorctl restart DjangoBlog
#关闭|启动|重启supervisor服务
sudo service supervisor stop | start | restart
此外,sudo supervisorctl
回车可以进入shell模式进行管理
写在后面
关于缓存问题的解决,就是把多线程替换为单线程,多线程下一个线程的内容更新了,但是其他的线程还是原来的样子,页面刷新的时候就会出现缓存问题。
如果发现没有superisor配置文件,请搜索如何安装supervisor(雾),请参考下面的内容:
1、使用命令创建:
#创建,如果提示权限不够请加sudo,还不行的话可以创建到普通目录,然后用mv命令移过去
echo_supervisord_conf > /etc/supervisor/supervisord.conf
#选择该文件,权限不够请加sudo
supervisord -c /etc/supervisor/supervisord.conf
2、自行创建:
sudo vim /etc/supervisor/supervisord.conf
贴入以下内容:
; supervisor config file
[unix_http_server]
file=/var/run/supervisor.sock ; (the path to the socket file)
chmod=0700 ; sockef file mode (default 0700)
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP)
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
; The [include] section can just contain the "files" setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*
; include files themselves.
[include]
files = /etc/supervisor/conf.d/*.conf
再使用supervisord -c /etc/supervisor/supervisord.conf
命令