最近在研究flask,在架设运行环境的时候犯了难。因为我想把每个独立的应用像NGINX处理多个网站那样,每个应用单独一个配置文件。而网上流传的uwsgi启动脚本都只支持单个配置文件。虽然有文章说可以把多个应用的配置写成命令集成到启动脚本里,但那样的话显然不够灵活。官方文档看了头实在是大,找来找去也没个头绪。于是决定自己把启动脚本改进一下。在原来脚本的基础上加入了配置文件遍历获取,再循环处理每个配置文件。改造难度不大效果却很好,完美实现我的需求。现将代码贴出来分享给有需要的人。当然如果您有更简便的方法能达到目的,还请劳烦告之一声。
特别声明:
1、 脚本只支持INI格式配置文件的加载,如需要加载其他格式配置文件请自行修改脚本中对应位置代码。
2、 PID文件名要求与配置文件名一致,扩展名为pid。如果不一样会导致进程不能正常关闭或重新加载。
3、 脚本命名为uwsgi_svr保存到/etc/init.d/目录下,记得配置执行权限。
#!/bin/bash # chkconfig: 2345 55 25 # Description: Startup script for uwsgi webserver on Debian. Place in /etc/init.d and # run 'update-rc.d -f uwsgi defaults', or use the appropriate command on your # distro. For CentOS/Redhat run: 'chkconfig --add uwsgi' ### BEGIN INIT INFO # Provides: uwsgi # Required-Start: $all # Required-Stop: $all # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: starts the uwsgi web server # Description: starts uwsgi using start-stop-daemon ### END INIT INFO # Modify by lykyl # Ver:1.1 # Description: script can loads multiple configs now. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DESC="uwsgi daemon" NAME=uwsgi_srv DAEMON=/usr/local/bin/uwsgi CONFIGDIR=/etc/uwsgi/ PIDDIR=/var/run/ SCRIPTNAME=/etc/init.d/$NAME FindCmd="/usr/bin/find" declare -a iniList declare -a SiteNameList function Init() { iniList=`$FindCmd $CONFIGDIR -name '*.ini'` for i in ${iniList[@]} do SiteNameList=(${SiteNameList[@]} `basename $i|awk -F. '{print $1}'`) done } function Start() { local c=0 for i in ${iniList[@]} do if $DAEMON $i; then echo "${SiteNameList[$c]} started" else echo "${SiteNameList[$c]} already running" fi let ++c done } function Stop() { local c=0 for i in ${SiteNameList[@]} do if $DAEMON --stop ${PIDDIR}${i}.pid; then echo "${SiteNameList[$c]} stoped" else echo "${SiteNameList[$c]} not running" fi rm -f ${PIDDIR}${i}.pid let ++c done } function Reload() { local c=0 for i in ${SiteNameList[@]} do if $DAEMON --reload ${PIDDIR}${i}.pid; then echo "${SiteNameList[$c]} reloaded" else echo "${SiteNameList[$c]} can't reload" fi let ++c done } function Status() { ps aux|grep $DAEMON echo } #main set -e [ -x "$DAEMON" ] || exit 0 Init case "$1" in status) echo -en "Status $NAME: "
Status ;; start) echo -en "Starting $NAME: " Start ;; stop) echo -en "Stopping $NAME: " Stop ;; reload|graceful) echo -en "Reloading $NAME: " Reload ;; *) echo "Usage: $SCRIPTNAME {start|stop|reload}" >&2 exit 3 ;; esac exit 0