一、概述

superviosr是一个Linux/Unix系统上的进程监控工具,他/她upervisor是一个Python开发的通用的进程管理程序,可以管理和监控Linux上面的进程,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。不过同daemontools一样,它不能监控daemon进程(也就是后台进程)

二、安装

apt-get install -y supervisor

安装成功后,会在/etc/supervisor目录下,生成supervisord.conf配置文件。

你也可以使用echo_supervisord_conf > supervisord.conf命令,生成默认的配置文件(不建议,内容比较多)。

supervisord.conf示例配置:

; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock ; (the path to the socket file)
chmod= ; sockef file mode (default ) [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

进程配置会读取/etc/supervisor/conf.d目录下的*.conf配置文件

安装完成之后,默认就启动了supervisor

三、管理uwsgi

在上一篇文章中,链接如下:

https://www.cnblogs.com/xiao987334176/p/11329906.html

已经配置好了uwsgi和nginx。这是2个比较关键的进程,任意一个进程死掉,都会导致网页无法访问。

修改uwsgi配置

关闭后台运行,为什么呢?因为supervisord无法管理后台进程

cd /www/mysite1/uwsgi
vim uwsgi.ini

注释掉daemonize

[uwsgi]

# Django-related settings
# the base directory (full path)
chdir = /www/mysite1
# Django's wsgi file
module = mysite1.wsgi
# the virtualenv (full path)
home = /virtualenvs/venv
# process-related settings
# master
master = true
# maximum number of worker processes
processes =
# pid file
pidfile = /www/mysite1/uwsgi/uwsgi.pid
# socket file path (full path)
socket = /www/mysite1/uwsgi/mysite1.sock
# clear environment on exit
vacuum = true
#The process runs in the background and types the log to the specified log file
#daemonize = /www/mysite1/uwsgi/uwsgi.log

关闭uwsgi

/virtualenvs/venv/bin/uwsgi --stop uwsgi.pid

新增uwsgi 进程配置文件

cd /etc/supervisor/conf.d
vim uwsgi.conf

内容如下:

[program:uwsgi]
directory = /www/mysite1 ;程序的启动目录
command= /virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini ;启动命令
autostart = true ; 在 supervisord 启动的时候也自动启动
startsecs = ; 启动 秒后没有异常退出,就当作已经正常启动了
autorestart = true ; 程序异常退出后自动重启
startretries = ; 启动失败自动重试次数,默认是
user = root ; 用哪个用户启动
redirect_stderr = true ; 把 stderr 重定向到 stdout,默认 false
stdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默认 50MB
stdout_logfile_backups = ; stdout 日志文件备份数 ;stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
stdout_logfile = /www/mysite1/logs/stdout.log
;输出的错误文件
stderr_logfile = /www/mysite1/logs/stderr.log
;添加运行需要的环境变量, 这里用了虚拟环境
;environment=PYTHONPATH=$PYTHONPATH:/virtualenvs/venv/bin/
;然后确保杀死主进程后,子进程也可以停止
stopasgroup=true
killasgroup=true

创建日志目录

mkdir /www/mysite1/logs/

注意:supervisord不会自动帮你创建目录,因此需要手动创建。

加载配置

supervisorctl reload

如果出现:

error: <class 'socket.error'>, [Errno ] No such file or directory: file: /usr/lib/python2./socket.py line: 

先要确认进程是否存在:ps -ef | grep supervisord 然后使用命令 supervisord -c /etc/supervisor/supervisord.conf 启动。

查看状态

第一个查看,状态为STARTING,第二次查看时,状态为RUNNING

root@ubuntu:/etc/supervisor/conf.d# supervisorctl status
uwsgi STARTING
root@ubuntu:/etc/supervisor/conf.d# supervisorctl status
uwsgi RUNNING pid , uptime ::

kill进程

root@ubuntu:/etc/supervisor/conf.d# ps -aux|grep uwsgi
root 0.3 0.8 ? S : : /virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini
root 0.0 0.7 ? S : : /virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini
root 0.0 0.0 pts/ S+ : : grep --color=auto uwsgi
root@ubuntu:/etc/supervisor/conf.d# killall -9 uwsgi
root@ubuntu:/etc/supervisor/conf.d# ps -aux|grep uwsgi
root 0.0 0.8 ? S : : /virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini
root 0.0 0.7 ? S : : /virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini
root 0.0 0.0 pts/ S+ : : grep --color=auto uwsgi

以上信息,可以发现。强制kill掉进程之后,supervisor会将uwsgi启动。

四、管理Nginx

由于supervisor不能监控后台程序,

command = /usr/local/bin/nginx 这个命令默认是后台启动,
加上-g ‘daemon off;’这个参数可解决这问题,这个参数的意思是在前台运行。

command = /usr/local/bin/nginx -g ‘daemon off;’

新增nginx 进程配置文件

cd /etc/supervisor/conf.d
vim nginx.conf

内容如下:

[program:nginx]
command = /usr/sbin/nginx -g 'daemon off;'
startsecs=
autostart=true
autorestart=true
stdout_logfile=/var/log/nginx/stdout.log
stopasgroup=true
killasgroup=true

加载配置

supervisorctl reload

查看状态

root@ubuntu:/etc/supervisor/conf.d# supervisorctl status
nginx RUNNING pid , uptime ::
uwsgi RUNNING pid , uptime ::

kill掉nginx进程

root@ubuntu:/etc/supervisor/conf.d# ps -aux|grep nginx
root 0.2 0.2 ? S : : nginx: master process /usr/sbin/nginx -g daemon off;
www-data 0.0 0.0 ? S : : nginx: worker process
root 0.0 0.0 pts/ S+ : : grep --color=auto nginx
root@ubuntu:/etc/supervisor/conf.d# killall -9 nginx
root@ubuntu:/etc/supervisor/conf.d# ps -aux|grep nginx
root 0.0 0.2 ? S : : nginx: master process /usr/sbin/nginx -g daemon off;
www-data 0.0 0.0 ? S : : nginx: worker process
root 0.0 0.0 pts/ S+ : : grep --color=auto nginx

本文参考:

https://www.cnblogs.com/xishuai/p/ubuntu-install-supervisor.html

https://blog.csdn.net/qq_32402917/article/details/80169366

https://blog.csdn.net/genglei1022/article/details/81239900

ubuntu supervisor管理uwsgi+nginx的更多相关文章

  1. supervisor管理uwsgi

    1. 前言 传统的管理uwsgi服务: 1. 通过shell脚本来编写start restart stop函数来控制 2. 比较麻烦,有时候控制写的烂,还会出现意想不到的错误 supervisor进行 ...

  2. Ubuntu下Django+uWSGI+nginx部署

    本文采用uwsgi+nginx来部署django 这种方式是将nginx作为服务端前端,将接受web所有的请求,统一管理,Nginx把所有的静态请求自己处理,然后把所有非静态请求通过uwsgi传递给D ...

  3. supervisor 管理uwsgi 进程

    Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动 重启.它是通过fork/exec的方式把这些被管理的进 ...

  4. linux之Ubuntu下Django+uWSGI+nginx部署

    http://www.chenxm.cc/post/275.html?segmentfault

  5. Ubuntu系统搭建django+nginx+uwsgi

    1. 在开发机上的准备工作 2. 在服务器上的准备工作 3.安装uwsgi 4.编写uwsgi配置文件,使用配置文件启动uwsgi 5. 安装nginx 6. 收集静态文件 7. 编写nginx配置文 ...

  6. Ubuntu环境下部署Django+uwsgi+nginx总结

    前言 这是我在搭建Django项目时候的过程,拿来总结记录,以备不时之需. 项目采用nginx+uwsgi的搭配方式. 项目依赖包采用requirements.txt文件管理的方式. 本地准备工作 确 ...

  7. 【云计算】使用supervisor管理Docker多进程-ntpd+uwsgi+nginx示例最佳实践

    supervisor安装启动: apt-get install supervisor -y # start supervisord nodaemon /usr/bin/supervisord --no ...

  8. 项目部署(ubuntu+uwsgi+nginx+supervisor+django)

    一.在开发机上的准备工作 1. 确认项目没有bug. 2.设置`ALLOW_HOST`为你的域名,以及ip地址. 4.设置`DEBUG=False`,避免如果你的网站产生错误,而将错误信息暴漏给用户. ...

  9. Flask+uwsgi+Nginx+Ubuntu部署

    学了一段时间flask,可是一直没有做过部署, 于是想着怎么部署呢, 想想,先吧服务给搞通吧,于是呢 就先想着去吧服务给搞起来,这里选择的是Flask+uwsgi+Nginx+Ubuntu, Pyth ...

随机推荐

  1. 理解 IO_WAIT 并且了解利用包括 top htop iotop iostat 工具来查看 IO 性能

    今天继续拜读「深入浅出计算机组成原理」专栏,觉得讲 IO_WAIT 这篇很有意思,正好可以结合前面的一篇讲物理硬件存速度的一块儿看. 现在我们看硬盘厂商出品的性能报告,通常会看到两个指标,一个是响应时 ...

  2. UE4破碎物体

    1. 创建可破碎物体 首先,启用插件: 然后,选择一个模型,右键,创建可破碎物体: 2. 创建蓝图 把新创建出来的物体创建为蓝图: 击碎物体的蓝图节点: 当然,要把那个物体(图上的Destructib ...

  3. linux高性能服务器编程 (七) --Linux服务器程序规范

    第七章 LInux 服务器程序规范 1)linux服务器程序一般以后台进程形式运行.后台进程又称为守护进程,是没有控制终端的,所以不会受到外界的干扰.守护进程的父进程通常是init进程(PID为1的进 ...

  4. 一个bug程序员的入园

    大家好,我叫dg是一个只写bug的程序员.当然只写bug也是有好处的,那就是踩过的坑多了,摔的跟斗多了,并且没有被摔死,勇敢的活了下来,练就了一身钢筋铁骨.哈哈,开个玩笑.但是猜的坑多了就知道了哪里有 ...

  5. 【BigData】Java基础_循环

    1.for循环 语法: for (初始表达式;布尔表达式;步进) { 循环体: } 实例: package cn.test.logan.day02; import java.util.Scanner; ...

  6. What is the difference between UNION and UNION ALL?

    What is the difference between UNION and UNION ALL? UNION removes duplicate records (where all colum ...

  7. Sublime Text 3.2.1详细安装破解教程,附最新激活码license(全网独家可用有效)

    title: "Sublime Text 3.2.1详细安装破解教程,附最新激活码license(全网独家可用有效)" categories: soft tags: soft au ...

  8. pip安装daemon模块

    E:\> pip install python-daemon Collecting python-daemon Downloading https://files.pythonhosted.or ...

  9. Data truncation: Out of range value for column 'quanity' at row 问题解决方案

    由于之前在自己电脑上搭建了mysql 5.6的数据库,但是在服务器上搭建的是mysql 5.7的环境,在运行过程中出现了如下错误: Data truncation: Out of range valu ...

  10. Synchronize深入

    前言:    synchronize会使用,但是对于深层次的知识,不是很清楚,故整理一篇博客. 简介:   能够保证在同一时刻,最多只有一个线程执行该端代码,以达到保证并发安全效果. 两种用法: 对象 ...