首页 运维干货利用 supervisor 轻松管理后台进程

利用 supervisor 轻松管理后台进程

运维派隶属马哥教育旗下专业运维社区,是国内成立最早的IT运维技术社区,欢迎关注公众号:yunweipai
领取学习更多免费Linux云计算、Python、Docker、K8s教程关注公众号:马哥linux运维

公司最近在做新的项目,前后端分离。前端改用node.js,后台的接口都做成了类似的微服务,比如注册一个服务,登录一个服务等等···直接打包成jar包启动,例如 java -jar xxx.jar

现在大概差不多有10多个左右的服务,今后会更多。

其实要放在后台执行用nohup 命令 在把日志重定向也可以做, nohup java -jar xxx.jar > /data/log/xxx.log 2>&1 &

但是管理起来就很麻烦了,google 了一下发型supervisor 很适合做这个事情。网上找了很多关于supervisor 的资料都不怎么完整,所以这里把笔记贴一下,方便日后自己使用

下载最新 supervisor 

Centos 6.5 可以使用 yum 安装,但是版本太老了。

我这里用的是 supervisor-3.1.3

可以到 supervisor 官网下载最新安装包

http://supervisord.org/

下载链接 http://pan.baidu.com/s/1c0yf7Te

安装 

tar zxvf supervisor-3.1.3

cd supervisor-3.1.3

python setup.py install

 

配置

echo_supervisord_conf  >/etc/supervisord.conf

vim /etc/init.d/supervisord

#! /bin/sh

# chkconfig: – 85 15

PATH=/sbin:/bin:/usr/sbin:/usr/bin

PROGNAME=supervisord

DAEMON=/usr/bin/$PROGNAME

CONFIG=/etc/$PROGNAME.conf

PIDFILE=/tmp/$PROGNAME.pid

DESC=”supervisord daemon”

SCRIPTNAME=/etc/init.d/$PROGNAME

# Gracefully exit if the package has been removed.

test -x $DAEMON || exit 0

start()

{

echo -n “Starting $DESC: $PROGNAME”

$DAEMON -c $CONFIG

echo “…”

}

stop()

{

echo -n “Stopping $DESC: $PROGNAME”

supervisor_pid=`ps -ef | grep supervisord | grep -v grep | awk ‘{print $2}’`

kill -15 $supervisor_pid

echo “…”

}

case “$1” in

start)

start

;;

stop)

stop

;;

restart)

stop

start

;;

*)

echo “Usage: $SCRIPTNAME {start|stop|restart}” >&2

exit 1

;;

esac

exit 0

 

chmod +x /etc/init.d/supervisord

chkconfig –add supervisord

chkconfig supervisord on

vim /etc/supervisord.conf

修改一下几行,去掉前面的注释“;”

[unix_http_server]
file=/tmp/supervisor.sock ; (the path to the socket file)
chmod=0700 ; socket file mode (default 0700)
;chown=nobody:nogroup ; socket file uid:gid owner
username=user ; (default is no username (open server))
password=123 ; (default is no password (open server))

[inet_http_server] ; inet (TCP) server disabled by default
port=0.0.0.0:9001 ; (ip_address:port specifier, *:port for all iface)
username=user ; (default is no username (open server))
password=123 ; (default is no password (open server))

[include]
files = /etc/supervisor.conf/*.conf

我们写一个测试的程序 hello.sh

vim /etc/supervisor.conf/hello.conf

[program:hello]
command= sh hello.sh
autostart=true ; supervisord守护程序启动时自动启动tornado
autorestart=true ; supervisord守护程序重启时自动重启tornado
redirect_stderr=true ; 将stderr重定向到stdout
user=root
directory=/www/sh ; cd 到应用目录
stdout_logfile = /tmp/hello.log

 

写一个测试脚本

vim /www/sh/hello.sh

#!/bin/bash
while true
do
echo “hello word”
sleep 10
done

chmox +x /www/sh/hello.sh

 

启动并测试

/etc/init.d/supervisord start

查看端口是否监听
lsof -i:9001

进入控制台

supervisorctl
Server requires authentication
Username:user
Password:

hello RUNNING pid 12665, uptime 0:00:59
supervisor>

 

说明hello.sh 已经在后台运行。

使用浏览器访问9001端口,查看WEB界面

 

 

 

本文链接:https://www.yunweipai.com/5481.html

网友评论comments

发表回复

您的电子邮箱地址不会被公开。

  1. 张庭说道:

    听老妈说的,真事!!一天四人在打麻将非要打到12点,一人不敢回家怕老婆。另外三个说 “走,我们陪你去,我就不相信我们四个大老爷们她还能把你吃了 “!到家后他老婆一把把他拉到卧室,呱唧呱唧两耳巴,门口三个吓得全跑了。

Copyright © 2012-2022 YUNWEIPAI.COM - 运维派 京ICP备16064699号-6
扫二维码
扫二维码
返回顶部