python程序使用1-supervisor管理进程
使用supervisor来管理进程,另外还设有screen也可以以
一、supervisor管理进程
1、安装
# easy_install supervisor
2、常用功能
*nix系统管理员常用supervisord和supervisorctl、
>>> import supervisor
1)、产生supervisord配置文件
可以通过简单的命令echo_supervisord_conf输出supervisord.conf的内容
产生配置文件
# echo_supervisord_conf > /etc/supervisord.conf
2)、写一个简单进程
#vim daemon.py
#!/usr/bin/env python
import time
print "Daemon run for 3 seconds,then dies"
time.sleep(3)
print "Daemon dies"
# chmod u+x /root/PythonDir/daemon.py
3)、配置这个进程
/etc/supervisord.conf 增加
[program:daemon]
command=/root/PythonDir/daemon.py
autorestart=true
4)启动supervisord
# supervisord
启动守护后daemon.py自动会启动,当关闭后又启动
# ps aux | grep python
root 22669 0.0 4.1 13340 10188 ?? Ss 1:42PM 0:00.12 /usr/local/bin/python2.7 /usr/local/bin/supervisord
root 22697 0.0 1.6 6636 3936 ?? S 1:43PM 0:00.11 python /root/PythonDir/daemon.py
5)查看运行状态
supervisor> exit
# supervisorctl
daemon RUNNING pid 22744, uptime 0:00:01
supervisor> ?
default commands (type help <topic>):
=====================================
add clear fg open quit remove restart start stop update
avail exit maintail pid reload reread shutdown status tail version
supervisor> status
daemon RUNNING pid 22902, uptime 0:00:02
supervisor> pid
22669
supervisord的pid
supervisor> tail
Error: too few arguments
查看程序的各种输出
tail [-f] <name> [stdout|stderr] (default stdout)
Ex:
tail -f <name> Continuous tail of named process stdout
Ctrl-C to exit.
tail -100 <name> last 100 *bytes* of process stdout
tail <name> stderr last 1600 *bytes* of process stderr
其他帮助
supervisor> ?clean
*** No help on clean
supervisor> ?clear
clear <name> Clear a process' log files.
clear <name> <name> Clear multiple process' log files
clear all Clear all process' log files
二、使用screen管理进程
分离常驻进程
# screen /usr/local/bin/tracd -b ofreebsd.skylog.cn -e /usr/local/www/apache22/data/trac/ -p 8888
Ctrl+A 分离
Ctrl+D 重新连接
# screen -r
There are screens on:
26665.pts-3.ofreebsd (Attached)
26548.pts-3.ofreebsd (Attached