cronなしで1分ごとにスクリプトを実行し、子プロセスを許可します。

cronなしで1分ごとにスクリプトを実行し、子プロセスを許可します。

私は2つのPHPスクリプトを書きました。 1つはワーカーで、もう1つはマスターです。

masterを手動で実行すると、Rabbitmqシステムを確認し、検出したキューごとに1つのワーカーを作成します。その後、マスターを再実行すると、各ワーカープロセスがまだ実行されていることを確認し、そうであれば何もしません。そうでなければ再起動します。

私はそれが非常に単純な概念だと思います。私は生成された各サブプロセスに対してマスターでnohupと&を使ってこれを行います。これで、60秒ごとにマスターを実行してキューが生きていることを確認し、そうでない場合は再作成すると推測できます。

これにより問題が発生します。 cronジョブではnohubを使用できず、&それ自体を使用するだけでは機能しないようです。私が知っている限り、スタッフは電話さえしませんでした。

別のシェルスクリプトを作成してmaster.phpを呼び出してみましたが、うまくいきませんでした。私はそれをupstartジョブとして生成しようとした後、ubuntu 17+からupstartが削除されたことを覚えていました。

私は他のアプローチに対する提案を開いていますが、どのパスを選択しても、master.phpがヘッドレスバックグラウンドプロセスとしてwork.phpファイルを生成できるようにする必要があります。

ベストアンサー1

#!/bin/bash
#I am just like a system daemon... 
#I do not need nohup for work, and I should not be called from cron.

#My name is main_safe
#I will become a separeted process even if my father dies... 
#i will check if main is still alive, and if dies i will restart it
#nohup is not needed inside shell script. 
#IMPORTANT: to die is very different from to freeze 
main_safe(){
trap "" HUP
while sleep 120; do
   main&
   wait 
done

}

#My name is main I like to keep restarting php master.php
#everytime it go away... remove wait and I will keep starting master.php with absolutely no reason.
#If you are paranoid you can program me to restart main_safe,
#But what will happen if you try to stop me? Bad things.. so... 
#IMPORTANT: to die is very different from to freeze 
main(){
trap "" HUP
while sleep 60; do

     php master.php & 
     #do whatever you want here

     #uncomment this to prevent two instances of master.php from going up maybe it is necessary:
     wait

done
}


#nohup is not needed in shell script
main_safe& 
pstree -p | grep $! 

これは許されますか?

おすすめ記事