重启后使用Upstart运行PHP脚本

时间:2015-12-21 20:40:15

标签: php ubuntu upstart

我有以下Upstart脚本。当我运行以下命令service worker-1 start时,一切都很完美。我可以看到正在运行ps aux | grep php的工人。我还可以使用service worker-1 stop来重启/更新工作人员。但不幸的是,这个脚本只能在重启时部分工作。该脚本已执行(开始:作业已在运行:当我尝试service worker-1 start时,worker-1)但我看不到任何正在运行的ps aux | grep php工作人员当然service worker-1 stop返回{{1} }}。你知道什么是错的吗?

stop: Unknown instance:

1 个答案:

答案 0 :(得分:1)

上面的脚本几乎是正确的。主要问题是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