国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php實現簡單的守護進程創建、開啟與關閉操作

2024-05-05 00:10:51
字體:
來源:轉載
供稿:網友

本文實例講述了php實現簡單的守護進程創建、開啟與關閉操作。分享給大家供大家參考,具體如下:

前提要安裝有pcntl擴展,可通過php -m查看是否安裝

<?phpclass Daemon {  private $pidfile;  function __construct() {    $this->pidfile = dirname(__FILE__).'/daemontest.pid';  }  private function startDeamon() {    if (file_exists($this->pidfile)) {      echo "The file $this->pidfile exists./n";      exit();    }    $pid = pcntl_fork();    if ($pid == -1) {      die('could not fork');    } else if ($pid) {      echo 'start ok';      exit($pid);    } else {    // we are the child      file_put_contents($this->pidfile, getmypid());      return getmypid();    }  }  private function start(){    $pid = $this->startDeamon();    while (true) {      file_put_contents(dirname(__FILE__).'/test.txt', date('Y-m-d H:i:s'), FILE_APPEND);      sleep(2);    }  }  private function stop(){    if (file_exists($this->pidfile)) {      $pid = file_get_contents($this->pidfile);      posix_kill($pid, 9);      unlink($this->pidfile);    }  }  public function run($argv) {    if($argv[1] == 'start') {      $this->start();    }else if($argv[1] == 'stop') {      $this->stop();    }else{      echo 'param error';    }  }}$deamon = new Daemon();$deamon->run($argv);

啟動

php deamon.php start

關閉

php deamon.php stop

希望本文所述對大家PHP程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 芷江| 栾川县| 钟祥市| 南溪县| 玛纳斯县| 屯门区| 综艺| 岳池县| 达州市| 泽州县| 伊春市| 上栗县| 蒙自县| 宣武区| 时尚| 新晃| 托里县| 茂名市| 宜兰县| 新乡市| 合山市| 东海县| 玉林市| 安国市| 鹤壁市| 沁阳市| 禹州市| 原平市| 育儿| 梓潼县| 西昌市| 泗阳县| 久治县| 昌都县| 沅陵县| 白河县| 顺平县| 沧州市| 寿光市| 桂林市| 翁源县|