2016-12-09 28 views
1

我有一個PHP腳本,它使用exec在Linux中啓動一些命令。這是一個簡單的wget,五分鐘後會死亡。我面對的問題是如果我做Control + c作爲腳本運行它不會死,直到我殺死wget的實際PID。我嘗試使用pcntl_signal以及使用exec/system/shell_exec等,他們都沒有工作。我使用的代碼是:PHP腳本不會死在控制+ c

所有的
<?PHP 
system('/usr/bin/timeout 300 wget -c --tries=0 --read-timeout=200 -O /tmp/ll.mp3 http://15323.live.streamtheworld.com:80/WABCAM_SC'); 
?> 
+0

我想這是因爲你殺死'/ usr/bin/timeout',而不是'wget'。你忘了分享你的'pcntl_signal'代碼: - ? –

+0

這似乎有道理(我只是在查殺超時)。我想在php中使用set_time_limit,但這會殺死我的整個腳本。我只想讓wget在X時間後結束。你也可以在下面看到我的pcntl代碼。 –

回答

0

首先,你必須申報ticks directive,使其在PHP 4.3.0和更新的工作(見manual page for pcntl_signal)。

然後,您必須註冊接收信號時調用的信號和回調函數。在你的情況下,你想要的信號SIGINTCTRL + C按下時產生。

declare(ticks = 1); 

// callback function called when signal is received 
function shutdown($signal) 
{ 
    echo 'Interrupted using CTRL + C'; 

    // you probably don't need this condition, just exit should be enough 
    if (($signal === SIGINT)) { 
     exit; 
    } 
} 

// register the callback function for the specific signal 
// SIGINT in this case is generated when you press CTRL + C 
pcntl_signal(SIGINT, 'shutdown'); 
+0

[root @ m -n〜]#php /tmp/l.php PHP致命錯誤:在第11行的/tmp/l.php中找不到類'Process_Sync' [root @ m-n〜]# –

+0

已編輯。請現在嘗試。 –

+0

下面也沒有工作。 <?PHP declare(ticks = 1); 函數關閉($信號) { echo'使用CTRL + C中斷'; //你可能不需要這個條件,只要退出就應該足夠了 if((signal signal)SIGINT){ exit; } } //註冊回調函數用於生成在這種情況下 // SIGINT所述特定信號時按CTRL + C pcntl_signal(SIGINT, '關閉'); system('/ usr/bin/timeout 3610 wget --tries = 0 --read-timeout = 200 -O /l.mp3 http://myurl.com:80/_SC'); ?> –