代码之家  ›  专栏  ›  技术社区  ›  Jiri Mihal

重启后使用Upstart运行PHP脚本

  •  1
  • Jiri Mihal  · 技术社区  · 9 年前

    我有下面的Upstart脚本。当我运行以下命令时 service worker-1 start ,一切都很完美。我可以看到跑步的工人 ps aux | grep php 。我也可以使用 service worker-1 stop 我需要重新启动/更新工作人员。但不幸的是,该脚本仅在重新启动时部分工作。脚本已执行(开始:作业已在运行:worker-1,当我尝试时 服务工作人员-1启动 )但我看不到任何带 ps辅助|grep php 当然 服务工作人员-1停止 返回 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
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   Jiri Mihal    9 年前

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