努力思考 + 实践, 眼高手低是不行的. 注册 | 登陆

守护进程删除smarty的缓存文件(注释版)

原文: 鸭嘴的Blog

偶参阅了网上资料加了注释.

PHP代码
  1. <?php   
  2.     
  3. define("PID_PATH""/tmp/delSmartyCache.pid");   
  4.   
  5. global $argv;   
  6. $cmd = $argv[1];   
  7.     
  8. switch($cmd) {   
  9.     case 'start' :   
  10.         do_work();   
  11.         break;   
  12.     case 'stop' :   
  13.         do_stop();   
  14.         break;   
  15.     case 'status' :   
  16.         show_status();   
  17.         break;   
  18.     default :   
  19.         show_usage();   
  20.         break;   
  21. }   
  22.   
  23. function do_work()   
  24. {   
  25.     // 创建子进程(守护进程)   
  26.     $newid = pcntl_fork();   
  27.     if ($newid == -1) {   
  28.         echo "pcntl_fork error\n";   
  29.         exit;   
  30.     }   
  31.     if ($newid) { // 返回值大于0, 表示创建成功, 提示信息并结束父进程   
  32.         echo "process delSmartyCache start successfully\n";   
  33.         exit;   
  34.     } else { // 返回值为0, 表示父进程结束后进入子进程的运行阶段   
  35.         // 取当前进程(子进程)的PID, 并写至进程文件   
  36.         $pid = posix_getpid();   
  37.         $fp = fopen(PID_PATH, "w");   
  38.         fwrite($fp$pid);   
  39.         fclose($fp);   
  40.         while (true) {   
  41.             $cmd = "rm -rf /elink/bbs_data/smarty/tmpl_c/*";   
  42.             $re = popen("$cmd""r"); // 打开一个指向删除进程的管道   
  43.             $ret = fread($re, 1024);   
  44.             if ($ret != "") {   
  45.                 // 将执行情况写入日志   
  46.                 $fp = fopen("/home/dexin/run_delSmartyCache.log","a+");   
  47.                 fwrite($fp$ret);   
  48.                 fclose($fp);   
  49.             }   
  50.             // 延时6秒   
  51.             sleep(6);   
  52.         }   
  53.     }   
  54. }   
  55.   
  56. // 停止守护进程   
  57. function do_stop()   
  58. {   
  59.     if (file_exists(PID_PATH)) {   
  60.         // 读取守护进程PID   
  61.         $fp = fopen(PID_PATH, "r");   
  62.         $pid = fread($fp, 10);   
  63.         // 退出守护进程   
  64.         $ret = posix_kill($pid, SIGQUIT);   
  65.         if ($ret === true) {   
  66.             echo "process delSmartyCache stopped successfully\n";   
  67.         } else {   
  68.             echo "fail stop process delSmartyCache\n";   
  69.         }   
  70.     } else {   
  71.         echo "process delSmartyCache not found\n";   
  72.     }   
  73. }   
  74.   
  75. // 使用说明   
  76. function show_usage()   
  77. {   
  78.     print   
  79. <<<EOF   
  80. Usage:   
  81.     php delSmartyCache.php <option>   
  82.   
  83. Options:   
  84.     start - Start the daemon   
  85.     stop - Stop daemon   
  86.     status - show the delSmartyCache daemon   
  87.     help - Show help   
  88. EOF;   
  89. }   
  90.   
  91. // 显示状态   
  92. function show_status()   
  93. {   
  94.     if (file_exists(PID_PATH)) {   
  95.         $fp = fopen(PID_PATH, 'r');   
  96.         $pid = fread($fp, 10);   
  97.         $ret = posix_kill($pid, 0); // 发送信号判断进程是否存活   
  98.         if($ret === true) {   
  99.             echo "process delSmartyCache is alive\n";   
  100.             exit;   
  101.         } else {   
  102.             echo "process delSmartyCache is down\nplease start the process\n";   
  103.             exit;   
  104.         }   
  105.     } else {   
  106.         echo "pid file not exists\n";   
  107.         exit;   
  108.     }   
  109. }   
  110.    

Tags: php, pcntl_fork

« 上一篇 | 下一篇 »

只显示5条记录相关文章

Notepad++运行PHP, Python (浏览: 768, 评论: 0)
Php ini_set ini_get可操作配置参数列表 (浏览: 4856, 评论: 1)
资料备忘,保持更新。 (浏览: 4167, 评论: 0)
40个迹象表明你还是PHP菜鸟 (浏览: 3142, 评论: 2)
注意$_SERVER['PHP_SELF']可能引起的跨站攻击. (浏览: 4990, 评论: 1)

Trackbacks

点击获得Trackback地址,Encode: UTF-8

发表评论

评论内容 (必填):