php守护进程
2016-07-13 11:39
691 查看
<?php set_time_limit(0); ini_set("memory_limit", "256M"); $pid = $_REQUEST['pid']; //可以启动多个进程 $pid_file = "{$pid}.pid"; //锁文件 $ctrl_file = "{$pid}.ctrl"; //控制文件 $idle_interval = 10000000; //空闲等待时间,微秒 //已有进程正在运行 if (file_exists($pid_file)) { //控制文件存在或者锁文件5分钟未被访问 $stop = file_exists($ctrl_file); clearstatcache(); if ($stop || ($_SERVER['REQUEST_TIME'] - fileatime($pid_file) >= 300)) { if ($pid = file_get_contents($pid_file)) { if ($stop) { @unlink($ctrl_file); } @unlink($pid_file); shell_exec("ps -ef | grep 'm=async&p=stat' | grep '{$pid}' | awk '{print \$2}' | xargs --no-run-if-empty kill"); //防止误杀 $logStr = date('Ymd H:i:s') . ($stop ? " Killed by control file: $pid.ctrl" : " Process has gone away: $pid.pid"); fc::debug($logStr, 'Async_Call'); } } }else { //第一次运行 if (!file_put_contents($pid_file, getmypid())) { die("Can not create PID file: {$pid_file}"); } $starttime = time(); while (1) { if(time() - $starttime >= 3600){ //每一小时重启一下 break; } touch($pid_file); //用于记录最后运行时间 // some code } }
相关文章推荐
- PHP的dl函数
- PHP7的安装
- Yii安装与使用Excel扩展的方法
- php通过system(),exec(),passthru()调用外部系统命令
- PHP设计模式-观察者
- ThinkPHP 开始事物操作
- RabbitMQ消息队列在PHP下的应用 (zhuan)
- ssh sftp scp命令
- Yii使用smsto短信接口的函数demo示例
- 欢迎使用CSDN-markdown编辑器
- 高性能php日志方案设想
- php 5.4中php-fpm 的重启、终止操作命令
- 开机启动ftp服务
- thinkphp 整合phpqrcode 生成二维码
- phpstudy虚拟主机配置
- PHP学习之路之Smarty变量
- SMTP协议--在cmd下利用命令行发送邮件
- php notes
- php学习笔记-1.登录页面的学习
- 解决ewebeditor for php在IE8下报editor.js错误的解决方案