*
zhyuzh3d
CentOS 下 python 程序的持续运行
使用 SSH 登录 CentOS 云服务器, python App.py 运行的程序在 SSH 登录退出后会自动结束
使用 nohup python App.py 运行的程序在 SSH 退出后仍能继续
使用 ctrl+z 把当前程序推到后台运行, 使用 fg 命令可以切回前台, ctrl+c 将终止当前程序
使用
nohup python App.py &
这个 & 符号将在 ctrl+c 后继续运行
Supervisor 的安装
supervisor 可以在 centoOS 下自动运行, 重启指定程序, 并进行有效管理
supervisor 目前只有 python2.x 版本
使用
virtualenv -p /usr/bin/python venv2x
创建一个 python2.x 的运行环境
然后
source venv2x/bin/activate
激活
然后
pip install supervisor
进行安装
Supervisor 的设置
创建配置文件,
echo_supervisord_conf> /etc/supervisord.conf
supervisord.conf 包含了 unix_http_server,supervisord,supervisorctl,include 等几部分, 分号开头表示注释内容
启动服务.
supervisord -c /etc/supervisord.conf
将启动服务
自动配置文件夹. 在 supervisor.conf 中启用 [include],
file=/etc/supervisord.d/*.conf
, 这将使 supervisor 自动加载此文件夹下的所有. conf 文件
创建配置文件夹.
mkdir /etc/supervisord.d/
增加一个配置文件. 在 supervisor.d 文件夹中添加 App.conf 文件, 内容包含以下内容:
- [program:App]
- command=python3 /tmp/App.py
然后重新启动服务.
supervisord -c /etc/supervisord.conf
. 如遇到问题, 可以
ps -A|grep supervisor
找到上一个程序的 pid, 然后 kill -9 xxx 结束上一个程序进程.
Supervisor 的进程管理
查看所有进程,
supervisorctl status all
停止或重启全部进程,
- supervisorctl stop all
- ,
- supervisorctl start all
为 App.conf 添加更多参数 autorestart=true 退出后自动重启, startretries=3 自动重启最多 3 次, startsecs=5 启动 5 秒后没退出视为启动成功
将 Supervisor 设置为随系统启动
CentOS 自动启动 / etc/rc.local 中设置的命令
echo "/usr/bin/supervisord -c /etc/supervisord.conf">> /etc/rc.local
将 Supervisor 命令加入命令列表
来源: http://www.jianshu.com/p/126e68e4cdbd