2015-12-21 133 views
1

我有以下的Upstart腳本。當我運行以下命令service worker-1 start時,一切正常。我可以看到ps aux | grep php運行的工人。我也可以使用service worker-1 stop我需要重新啓動/更新工人。但不幸的是,這個腳本僅在重啓時部分工作。腳本被執行(開始:作業已經運行:worker-1當我嘗試service worker-1 start)但我沒有看到任何正在運行的工作人員ps aux | grep php,當然service worker-1 stop返回stop: Unknown instance:。你有什麼想法可能是錯的?重啓後運行PHP腳本與Upstart

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 
+0

只是一個猜測,但在重新啓動我會假設用戶在運行啓動腳本/服務是不是你很可能根本。您是否嘗試過使用sudo甚至su來運行命令來root並運行它。 –

+0

是的,我試過了,我可以用sudo運行上面的命令,沒有任何問題。我也可以以root用戶身份運行這些命令。但我可以嘗試更改從用戶文件夾到某個「系統」文件夾的路徑。也許用戶的文件夾在啓動早期不可用。 –

+0

嘗試使用像這樣(http://askubuntu.com/questions/20229/how-to-run-an-application-at-startup-as-a-certain-user)將用戶啓動時切換到另一個用戶(你自己)並運行該用戶下的啓動命令/服務。 –

回答

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