Improve the uwsgi STARTUP script so that it supports multiple independent configuration files, uwsgi configuration files
Recently, I have been studying flask and have made some difficulties in setting up the runtime environment. Because I want to process each independent application as NGINX processes multiple websites, and each application has a single configuration file. The uwsgi startup scripts circulating on the Internet only support a single configuration file. Although some articles say that the configuration of multiple applications can be written as commands and integrated into the startup script, it is obviously not flexible. The official document is too big to look. So I decided to improve the startup script myself. The configuration file traversal is added to the original script to recycle each configuration file. The transformation is not very difficult, but it is very good. It perfectly meets my needs. Now I will post the code and share it with people who need it. Of course, if you have a simpler method to achieve your goal, please let us know.
Note:
1. The script only supports the loading of configuration files in INI format. To load configuration files in other formats, modify the corresponding code in the script.
2. The PID file name must be consistent with the configuration file name. The extension is pid. If they are different, the process cannot be properly shut down or reloaded.
3. Name the script uwsgi_svr and save it to the/etc/init. d/directory. Remember to configure the execution permission.
#!/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/binDESC="uwsgi daemon"NAME=uwsgi_srvDAEMON=/usr/local/bin/uwsgiCONFIGDIR=/etc/uwsgi/PIDDIR=/var/run/SCRIPTNAME=/etc/init.d/$NAMEFindCmd="/usr/bin/find"declare -a iniListdeclare -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}#mainset -e[ -x "$DAEMON" ] || exit 0Init 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