User-Profile-Image
hankin
  • 5
  • 首页
  • 分类
    • Windows
    • Web
    • VoIP
    • Virtualization
    • Trading
    • Smart Home
    • SEO
    • Security
    • Python
    • Program
    • PHP
    • OSX
    • OS
    • OpenSource
    • Nginx
    • MySQL
    • Mood
    • Linux
    • Life
    • Injection
    • Hosting
    • Finance
    • Exploits
    • CDN
  • 页面
    • About
  • 友链
    • 维酷科技
    • Tesra超算网络
Help?

Please contact us on our email for need any support

Support
  • 首页
    首页   ›   OS   ›   Linux   ›   正文
Linux

Supervisor守护Python进程

2017-01-14 17:40:05
2451  0 0

Debian下使用supervisor进程守护的方法

安装supervisor
apt-get install supervisor
配置supervisor进程守护
在目录/etc/supervisor/conf.d/下, 新建一个文件,名字:yourpython.conf
在yourpython.conf文件里编辑添加:

[program:yourpython]
directory=/root/yourpython
command=python server.py -c /root/yourpython/user-config.json
user=root
autostart = true
autoresart = true
stderr_logfile = /var/log/yourpython.log
stdout_logfile = /var/log/yourpython.log
stderr_logfile_maxbytes=4MB
stderr_logfile_backups=10
startsecs=3
command就是ss的运行命令;
user是执行命令的用户;
autostart和autoresart指自动启动和自动重启;
stdout_logfile重定向程序输出到此文件;
stderr_logfile重定向程序错误到此文件;
修改以下文件
/etc/profile
/etc/default/supervisor

在文件结尾处添加以下3行内容

ulimit -n 51200
ulimit -Sn 4096
ulimit -Hn 8192
启动supervisor
service supervisor start #启动
supervisorctl reload #重载
debug查看连接日志等
supervisorctl tail -f yourpython stderr #Ctrl+C 取消查看

CentOS使用supervisor

安装easy_install supervisor
运行echo_supervisord_conf测试是否安装成功。
创建配置文件:
echo_supervisord_conf > /etc/supervisord.conf
修改配置文件:
在supervisord.conf最后增加:

[program:yourpython]
directory=/root/yourpython
command = python server.py -c /root/yourpython/user-config.json
user=root
autostart=true
autorestart=true
stderr_logfile = /var/log/yourpython.log
stdout_logfile = /var/log/yourpython.log
startsecs=3
使用指定配置文件启动:/usr/bin/supervisord -c /etc/supervisord.conf
-c 表示配置文件的路径,读取这里个配置文件,之前也是可以根据自己的情况放在不同的文件夹下
修改配置文件之后:supervisorctl reload 重载 服务重新启动
debug查看连接日志:supervisorctl tail -f yourpython stderr #Ctrl+C 取消查看
设置supervisord开机启动
编辑文件:vi /etc/rc.local
在末尾另起一行添加supervisord,保存退出(和上文类似)。
另centos7还需要为rc.local添加执行权限
chmod +x /etc/rc.local
至此运用supervisord控制yourpython开机自启和后台运行设置完成
常用命令
控制命令基本都通过supervisorctl执行,输入help可以看到命令列表。这是一些常用命令:
获得所有程序状态 supervisorctl status
关闭目标程序 supervisorctl stop spider
启动目标程序 supervisorctl start spider
关闭所有程序 supervisorctl shutdown

定时reload

执行crontab -e,添加以下内容

0 * * * * supervisorctl reload

表示每小时进行reload一次。

评论 (0)

Click here to cancel reply.

欢迎您 游客  

    ProdigYu
    180文章 77评论 4点赞 554035浏览
    随机文章
    开源iptables Web管理系统
    5年前
    CentOS安装VNC发生一个奇葩的错误
    11年前
    Top 10 Opensource CRM
    15年前
    最近弄的一个网站
    13年前
    Python实现Ping
    15年前
    Tags
    Apache CentOS CMD DDOS Debian DNS Docker FastCGI Google IIS IP iptables Linux Mac MySQL Nginx Opensource PHP Python Skill SSH Ubuntu Vim VMware VPN web.py Windows 删除 命令 命令提示符 安装 开源 技巧 插件 漏洞 百度 系统 组件 终端 缓存 网络电话 脚本 自建 详解 错误
    Meta
    • Log in
    • Entries feed
    • Comments feed
    • WordPress.org
    Copyright © 2025
    主页
    页面
    • About
    博主
    ProdigYu 管理员
    Something about myself
    180 文章 77 评论 554035 浏览
    测试
    测试