0%

gunicorn 启动脚本

gunicorn 是python web app比较好用的wsgi,下面是一段平时快速启动的shell script。
把代码保存成 gunicorn.sh ,添加执行权限 sudo chmod +x gunicorn.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#解释下几个参数:
# -w 设置工作者数,推荐为cpu核心数*2+1
# --reload app代码更新自动重装
# -u 启动用户
# --log-file 日志输出目录
# -k 使用其他异步模块启动如gevent
# -D 作为deamon进程启动
P=5000
worker=3
host="127.0.0.1"
case "$@" in
start)
gunicorn -b $host:$P -w $worker --reload -u root --log-file /var/log/gunicorn/gunicorn.log -k gevent -D app:app
;;
stop)
kill -9 `ps aux|grep gunicorn|grep $name|awk '{print $2}'|xargs`
;;
status)
pids=$(ps aux|grep gunicorn|grep $name)
echo "$pids"
;;
restart)
kill -9 `ps aux|grep gunicorn|grep $name|awk '{print $2}'|xargs`
sleep 1
gunicorn -b $host:$P -w $worker --reload -u root --log-file /var/log/gunicorn/gunicorn.log -k gevent -D app:app
;;
reload)
ps aux |grep gunicorn |grep $name | awk '{print $2}'|xargs kill -HUP
;;
*)
echo 'unknown arguments args(start|stop|status|restart|reload)'
exit 1
;;
esac

gunicorn.sh放到wsgi启动程序的目录,例子中wsgi程序是app.py
接着启动只需
gunicorn.sh start
停止
gunicorn.sh stop
查看进程
gunicorn.sh status
重载
gunicorn.sh reload
重启
gunicorn.sh restart

用起来有是不是有种nginx的感觉:) ,更多gunicorn相关设置可以到官网文档查看