最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php實現(xiàn)簡單的守護進程創(chuàng)建、開啟與關(guān)閉操作

 更新時間:2019年08月13日 08:37:49   作者:fangdong88  
這篇文章主要介紹了php實現(xiàn)簡單的守護進程創(chuàng)建、開啟與關(guān)閉操作,結(jié)合實例形式分析了基于pcntl擴展的php守護進程類定義、啟動及關(guān)閉等相關(guān)操作技巧,需要的朋友可以參考下

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

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

<?php
class 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

關(guān)閉

php deamon.php stop

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP進程與線程操作技巧總結(jié)》、《PHP網(wǎng)絡編程技巧總結(jié)》、《PHP基本語法入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

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

相關(guān)文章

最新評論

成武县| 彰武县| 浦东新区| 门源| 五河县| 南澳县| 伊吾县| 麻阳| 庆安县| 瑞安市| 鹿邑县| 磐安县| 建平县| 辽阳县| 独山县| 古丈县| 三明市| 囊谦县| 马鞍山市| 阳高县| 吐鲁番市| 安庆市| 稻城县| 长岭县| 洛隆县| 乡宁县| 宁远县| 敦煌市| 新蔡县| 紫阳县| 漳浦县| 宁明县| 河北省| 上林县| 锡林郭勒盟| 攀枝花市| 调兵山市| 商城县| 伊宁县| 松原市| 邵武市|