运行 PHP 重新启动后带有 Upstart 的脚本
Run PHP script with Upstart after reboot
我有以下 Upstart 脚本。当我 运行 以下命令 service worker-1 start
时,一切正常。我可以看到 运行ning 工人和 ps aux | grep php
。我还可以使用 restart/update 工作人员需要的 service worker-1 stop
。但不幸的是,这个脚本在重启时只能部分工作。脚本已执行(开始:当我尝试 service worker-1 start
时,工作已经 运行ning: worker-1)但我没有看到任何 运行ning worker with ps aux | grep php
和课程 service worker-1 stop
returns stop: Unknown instance:
。你知道哪里出了问题吗?
description "Starts/kills workers."
author "Jiri Mihal"
start on (started php5-fpm and started mysql)
stop on shutdown
pre-start script
echo "[`date`] Workers started" >> /var/log/worker-1.log
exec 2>>/var/log/worker-1.log
end script
post-start script
echo $$ > /var/run/worker-1.pid
for i in `seq 1 5`;
do
exec php /home/jiri/workers/dlapi.workers/workers/RpcWorkerLauncher.php Worker-1 >/dev/null 2>&1 &
done
end script
post-stop script
read -r FIRSTLINE < /var/run/worker-1.pid
kill $(($FIRSTLINE + 2))
kill $(($FIRSTLINE + 3))
kill $(($FIRSTLINE + 4))
kill $(($FIRSTLINE + 5))
kill $(($FIRSTLINE + 6))
rm /var/run/worker-1.pid
echo "[`date`] Workers stopped" >> /var/log/worker-1.log
end script
上面的脚本几乎是正确的。主要问题是 PHP 脚本启动了 RabbitMQ worker 但 RabbitMQ 服务器还没有准备好。
我做了一些额外的调整,这是一个可行的解决方案:
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
我有以下 Upstart 脚本。当我 运行 以下命令 service worker-1 start
时,一切正常。我可以看到 运行ning 工人和 ps aux | grep php
。我还可以使用 restart/update 工作人员需要的 service worker-1 stop
。但不幸的是,这个脚本在重启时只能部分工作。脚本已执行(开始:当我尝试 service worker-1 start
时,工作已经 运行ning: worker-1)但我没有看到任何 运行ning worker with ps aux | grep php
和课程 service worker-1 stop
returns stop: Unknown instance:
。你知道哪里出了问题吗?
description "Starts/kills workers."
author "Jiri Mihal"
start on (started php5-fpm and started mysql)
stop on shutdown
pre-start script
echo "[`date`] Workers started" >> /var/log/worker-1.log
exec 2>>/var/log/worker-1.log
end script
post-start script
echo $$ > /var/run/worker-1.pid
for i in `seq 1 5`;
do
exec php /home/jiri/workers/dlapi.workers/workers/RpcWorkerLauncher.php Worker-1 >/dev/null 2>&1 &
done
end script
post-stop script
read -r FIRSTLINE < /var/run/worker-1.pid
kill $(($FIRSTLINE + 2))
kill $(($FIRSTLINE + 3))
kill $(($FIRSTLINE + 4))
kill $(($FIRSTLINE + 5))
kill $(($FIRSTLINE + 6))
rm /var/run/worker-1.pid
echo "[`date`] Workers stopped" >> /var/log/worker-1.log
end script
上面的脚本几乎是正确的。主要问题是 PHP 脚本启动了 RabbitMQ worker 但 RabbitMQ 服务器还没有准备好。
我做了一些额外的调整,这是一个可行的解决方案:
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