php 後台執行,PHP開啟守護程序後台執行

2021-10-25 21:28:27 字數 1560 閱讀 7573

只能在linux環境執行有時候難免要用到php守護程序,需要在cli模式下保持執行,直接一段**即可,// linux php守護程序/啟動/停止/重啟/查詢狀態

class servicedeamon  else  else if ($pid > 0)  else  else  else  else  else {

echo "process not running yet!" . php_eol;

public function run($ar**) {

$param = is_array($ar**) && count($ar**) == 2 ? $ar**[1] : null;

switch ($param) {

case 'start':

$this->start();

break;

case 'stop':

$this->stop();

break;

case 'reload':

$this->reload();

break;

case 'status':

$this->status();

break;

default:

echo "unknown command!" . php_eol .

"usage: " . $ar**[0] . " start|stop|reload|status" . php_eol;

break;

public function addservice($servicename, callable $servicecallback) {

$this->tasks[$servicename] = closure::bind($servicecallback, $this, get_class());

// add your own task

$servicedeamon = new servicedeamon();

$servicedeamon->addservice('test', function(){

$i = 0;

while(true) {

++$i;

//echo $i . ': this is a deamon' . php_eol;

file_put_contents(

'echo_txt.txt',

$i.': this is a deamon'.date('y-m-d h:i:s').php_eol,

sleep(10);

$servicedeamon->run($ar**);

to start:

localhost:~$ php run.php start

//localhost:~$ php run.php start&

to stop:

localhost:~$ php run.php stop

to check the status:

localhost:~$ php run.php status

to reload:

localhost:~$ php run.php reload

PHP實現守護程序方式,Linux後台執行

linux後台執行程式,nohup 和 結合使用 使用 後台執行程式 使用nohup執行程式 缺點 依賴終端 此時使用fg命令可以恢復前台執行 ps 後台程序並未完全脫離終端,在終端未關閉前還是會往終端輸出結果,後台程式會隨shell退出而停止 缺點 占用終端 用途 不結束通話地執行命令,即使終端s...

PHP實現守護程序方式,Linux後台執行

linux後台執行程式,nohup 和 結合使用 使用 後台執行程式 使用nohup執行程式 缺點 依賴終端 此時使用fg命令可以恢復前台執行 ps 後台程序並未完全脫離終端,在終端未關閉前還是會往終端輸出結果,後台程式會隨shell退出而停止 缺點 占用終端 用途 不結束通話地執行命令,即使終端s...

PHP實現守護程序方式,Linux後台執行

linux後台執行程式,nohup 和 結合使用 使用 後台執行程式 使用nohup執行程式 缺點 依賴終端 此時使用fg命令可以恢復前台執行 ps 後台程序並未完全脫離終端,在終端未關閉前還是會往終端輸出結果,後台程式會隨shell退出而停止 缺點 占用終端 用途 不結束通話地執行命令,即使終端s...