posixkill php,在linux中使用PHP的posix_kill()会导致奇怪的行为
我正在創(chuàng)建一個網(wǎng)頁,用于監(jiān)視和控制一些自定義C程序.我創(chuàng)建了一個頁面,它將啟動/停止一個C程序’啟動器'(漂亮的通用名稱),然后分叉并創(chuàng)建許多子進(jìn)程.開始工作正常 – exec(“cd launcher_dir; nohup ./launcher> outfile 2>& 1&”);
停止是有問題的地方.單擊停止按鈕后,隨機(jī)發(fā)生兩件事之一.有一個瀏覽器錯誤頁面(101連接重置或324空響應(yīng))或頁面加載兩次,但您只能第二次看到它.我知道它加載兩次的原因是因為代碼中的調(diào)試消息.在這兩種情況下,啟動程序進(jìn)程都被終止(發(fā)送SIGTERM).但是如果頁面加載兩次,則第一次殺死啟動器(此部分的頁面上沒有任何內(nèi)容),第二次檢查并發(fā)現(xiàn)沒有啟動器進(jìn)程正在運行并顯示一條消息:“啟動器未運行”.
我正在將調(diào)試消息寫入文件,發(fā)現(xiàn)重新加載發(fā)生在PHP代碼中的某個變量行(有時會打印某個調(diào)試消息,有時則不會打印.)此外,PHP錯誤報告設(shè)置為ALL沒有錯誤.
Launcher捕獲SIGTERM,依次將SIGTERM發(fā)送到它的子進(jìn)程,然后調(diào)用exit(0).
有趣的是,如果SIGKILL用于殺死啟動器,那么PHP工作正常并且符合預(yù)期,但是這不允許啟動器很好地關(guān)閉.這可能發(fā)生什么?
這是相關(guān)的PHP代碼:
function stop_launcher(){
$pid = get_launcher_pid(); // Definitely returns the correct pid
debug_message("stop_launcher called,pid = ".$pid);
if ($pid == "") {
// If no 'connection reset' error occurs this is displayed
// after first executing the else branch. Why is the PHP being run twice?
print "Launcher doesn't seem to be running..
";
exit;
} else {
debug_message("killing");
posix_kill(intval($pid),15); //SIGTERM
debug_message("kill finished"); // Sometimes this message is written,sometimes not
if (ps_exists($pid)) { // Definitely works. This never gets displayed
print "Shutdown Failed. Try again";
exit;
}
}
}
function debug_message($message){
$fh = fopen(".debug",'a') or die("can't open file");
fwrite($fh,date("-r").": ".$message."\n");
fclose($fh);
}
任何建議表示贊賞!
總結(jié)
以上是生活随笔為你收集整理的posixkill php,在linux中使用PHP的posix_kill()会导致奇怪的行为的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java品酒会,我学 rxjava 2(
- 下一篇: php 处理html,PHP解析HTML