gpt4 book ai didi

django - 如何在启动时可靠地启动多个 DJango FCGI 服务器?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:27:21 24 4
gpt4 key购买 nike

我目前使用以下脚本来启动我的 DJango FCGI 服务器:

#!/bin/bash
MYAPP=$1
PIDFILE=/var/run/${MYAPP}_fcgi.pid
SOCKET=/var/django/${MYAPP}/socket.sock
MANAGESCRIPT=/var/django/${MYAPP}/manage.py
# Maximum requests for a child to service before expiring
#MAXREQ=
# Spawning method - prefork or threaded
#METHOD=
# Maximum number of children to have idle
MAXSPARE=2
# Minimum number of children to have idle
MINSPARE=1
# Maximum number of children to spawn
MAXCHILDREN=3

cd "`dirname $0`"

function failure () {
STATUS=$?;
echo; echo "Failed $1 (exit code ${STATUS}).";
exit ${STATUS};
}

function start_server () {
$MANAGESCRIPT runfcgi socket=$SOCKET pidfile=$PIDFILE \
${MAXREQ:+maxrequests=$MAXREQ} \
${METHOD:+method=$METHOD} \
${MAXSPARE:+maxspare=$MAXSPARE} \
${MINSPARE:+minspare=$MINSPARE} \
${MAXCHILDREN:+maxchildren=$MAXCHILDREN} \
${DAEMONISE:+damonize=True}
touch $SOCKET
chown www-data:www-data $SOCKET
chmod 755 $SOCKET
}

function stop_server () {
if [ -f "$PIDFILE" ]
then
kill `cat $PIDFILE` || failure "Server was not running."
rm $PIDFILE
fi
}

DAEMONISE=$3

case "$2" in
start)
echo -n "Starting fcgi: "
[ -e $PIDFILE ] && { echo "PID file exsts."; exit; }
start_server || failure "starting fcgi"
echo "Done."
;;
stop)
echo -n "Stopping fcgi: "
[ -e $PIDFILE ] || { echo "No PID file found."; exit; }
stop_server
echo "Done."
;;
restart)
echo -n "Restarting fcgi: "
[ -e $PIDFILE ] || { echo -n "No PID file found..."; }
stop_server
start_server || failure "restarting fcgi"
echo "Done."
;;
*)
echo "Usage: $0 {start|stop|restart} [--daemonise]"
;;
esac

exit 0

我是这样手动调用的:

/var/django/server.sh mysite start

这工作正常,但是当我的托管公司重新启动我们的服务器时,它给我留下了两个问题:

  1. 我没有自动启动多个网站的方法。
  2. 我最终得到一个存在但没有关联进程的 mysite_fcgi.pid 文件。

所以我有两个问题:

  1. 如何在启动时自动启动站点列表(存储在纯文本文件中)?即调用 /var/django/server.sh mysite1 start 然后调用 /var/django/server.sh myothersite start
  2. 如果进程不存在并尝试正常启动服务器,我如何摆脱 .pid 文件?

最佳答案

  1. 创建一个初始化脚本并将其分配给适当的运行级别。
  2. 您需要在您的启动/初始化脚本(您将在第 1 步中编写)中实现它

或者,使用流程管理器,如 supervisord这会解决您所有的顾虑。

这是来自 supervisord 的 fcgi 配置示例。

[fcgi-program:fcgiprogramname]
command=/usr/bin/example.fcgi
socket=unix:///var/run/supervisor/%(program_name)s.sock
process_name=%(program_name)s_%(process_num)02d
numprocs=5
priority=999
autostart=true
autorestart=unexpected
startsecs=1
startretries=3
exitcodes=0,2
stopsignal=QUIT
stopwaitsecs=10
user=chrism
redirect_stderr=true
stdout_logfile=/a/path
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=10
stderr_logfile=/a/path
stderr_logfile_maxbytes=1MB
stderr_logfile_backups
environment=A=1,B=2

关于django - 如何在启动时可靠地启动多个 DJango FCGI 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9563095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com