最近在研究flask,在架設運行環境的時候犯了難。由於我想把每一個獨立的應用像NGINX處理多個網站那樣,每一個應用單獨一個配置文件。而網上流傳的uwsgi啓動腳本都只支持單個配置文件。雖然有文章說能夠把多個應用的配置寫成命令集成到啓動腳本里,但那樣的話顯然不夠靈活。官方文檔看了頭實在是大,找來找去也沒個頭緒。因而決定本身把啓動腳本改進一下。在原來腳本的基礎上加入了配置文件遍歷獲取,再循環處理每一個配置文件。改造難度不大效果卻很好,完美實現個人需求。現將代碼貼出來分享給有須要的人。固然若是您有更簡便的方法能達到目的,還請勞煩告之一聲。web
特別聲明:flask
一、 腳本只支持INI格式配置文件的加載,如須要加載其餘格式配置文件請自行修改腳本中對應位置代碼。bash
二、 PID文件名要求與配置文件名一致,擴展名爲pid。若是不同會致使進程不能正常關閉或從新加載。app
三、 腳本命名爲uwsgi_svr保存到/etc/init.d/目錄下,記得配置執行權限。ide
#!/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: \n" Status ;; start) echo -en "Starting $NAME: \n" Start ;; stop) echo -en "Stopping $NAME: \n" Stop ;; reload|graceful) echo -en "Reloading $NAME: \n" Reload ;; *) echo "Usage: $SCRIPTNAME {start|stop|reload}" >&2 exit 3 ;; esac exit 0