上面的脚本几乎是正确的。主要问题是PHP脚本启动了RabbitMQ工作程序,但RabbitMQserver尚未就绪。
我做了一些额外的调整,下面是一个有效的解决方案:
description "Starts/kills workers."
author "Jiri Mihal"
start on (rabbitmq-server-running or started rabbitmq-server)
stop on (shutdown or rabbitmq-server-stopped or stopping rabbitmq-server)
env WORKER=Workername
env COUNT=5
pre-start script
echo "[`date`] Workers started" >> /var/log/worker-$WORKER.log
end script
post-start script
for i in `seq 1 $COUNT`;
do
exec php /home/jiri/workers/dlapi.workers/workers/RpcWorkerLauncher.php $WORKER >/dev/null 2>&1 &
if [ $i = 1 ]; then
echo $! > /var/run/worker-$WORKER.pid
fi
done
end script
post-stop script
read -r PID < /var/run/worker-$WORKER.pid
for i in `seq 1 $COUNT`;
do
kill $(($PID + $i - 1))
done
rm /var/run/worker-$WORKER.pid
echo "[`date`] Workers stopped" >> /var/log/worker-$WORKER.log
end script