php虚拟主机下实现定时任务
浏览量:224
why:
how:
开启无限循环(定时任务) :确保timed_task_config.txt 存在 并且内容为空 访问timed_task.php 等待页面反馈 如果页面进入卡住状态 则直接查看browse_log.php 看看日志里面是否记录启动成功
关闭无限循环(定时任务) :将timed_task_config.txt的内容改为close 然后等待一会 查看browse_log.php 看看日志里面是否记录关闭 记录了关闭才算关闭成功
日志查看:查看browse_log.php 有一点很关键注意看上下2个正常日志的时间间隔是否是符合你的要求 如果出现一些误差比较大的时间差 那么可以考虑是不是多开了无限循环 可以全部关闭重开下
主文件timed_task,php的代码
<?php
ignore_user_abort(); //函数设置与客户机断开是否会终止脚本的执行
set_time_limit(0); // 来设置一个脚本的执行时间为无限长
date_default_timezone_set(‘PRC‘); // 切换到中国的时间
$interval=20;//间隔多久执行一次
$timed_task_config_path="timed_task_config.txt";//负责让这个无限循环停止的一个开关 其实是控制定时任务机制的一个配置文件 如果该文件不存在或者 里面的内容为close 那么定时任务不会执行 如果为open 则会开启一个无限循环来进行监控和执行定时任务
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>定时任务</title>
</head>
<body>
<?php
//问题1 如何控制这些无限循环让他们停止
//问题2 如何避免打开多个无限循环增加服务器的资源消耗
//问题3 无限循环的日志监控和避免无限增加的处理办法
//控制用txt
//php定时计划任务
//检测定时任务的配置文件 是否存在或者里面的内容 //strpos($str1,"open")
if(file_exists($timed_task_config_path))
{
//存在配置文件 开始检测里面的内容
$str1="";
$str1=trim(file_get_contents($timed_task_config_path));
if (is_numeric(strpos($str1,"open"))) {
////1.内容为open 表示当前已经有定时任务线程在运行了 就不再额外执行定时任务
send_timed_task_log("已经有一个定时任务进程存在 不能开启其他的进程");
exit("已经有一个定时任务进程存在 不能开启其他的进程");
} elseif (is_numeric(strpos($str1,"close"))) {
////2.内容为close表示要求定时任务关闭 更不用
send_timed_task_log("配置文件要求关闭所有定时任务进程");
exit("配置文件要求关闭所有定时任务进程");
} else {
////3.如果不是open 和close 那么就锁定该文件然后修改里面的内容为open 修改成功
$fp = fopen($timed_task_config_path, ‘w+‘);
if(!is_writable($timed_task_config_path)){
send_timed_task_log("无法写入配置文件 请刷新重试");
exit("<p>无法写入配置文件 请刷新重试</p>");
}
flock($fp, LOCK_EX);
fwrite($fp, ‘open‘);
sleep(1);
flock($fp, LOCK_UN);
fclose($fp);
//////////如果再次读取下配置文件内容判断是否写入成功 成功则开始执行后面的无限循环 不成功提示并结束网页
$str1="";
$str1=trim(file_get_contents($timed_task_config_path));
// echo "<p>".$str1." </p>";
// echo "<p>".strpos($str1,"open")." </p>";
// exit();
if (is_numeric(strpos($str1,"open"))) {
///////////////修改成功 获取到了定时任务进程的权利
send_timed_task_log("启动成功 请关闭网页");
do{
$body="";
if (file_exists($timed_task_config_path)) {
$body = trim(file_get_contents($timed_task_config_path));
if (is_numeric(strpos($body,"close")))
{
send_timed_task_log("配置文件要求关闭所有的定时任务");
exit("<p>配置文件要求关闭所有的定时任务</p>");
}
} else {
send_timed_task_log("没有找到定时任务配置文件");
exit("<p>没有找到定时任务配置文件 进程结束</p>");
}
//-----------------------定时操作开头-------------------------------
send_timed_task_log("<常规检测:一切正常>");
//-----------------------定时操作结尾-------------------------------
sleep(rand($interval-2,$interval+2)); // 函数延迟代码执行若干秒 为了避免完全一样 随机下间隔避免一些问题
}while(true);
}else{
send_timed_task_log("没有获取到定时任务进程的权限 请刷新重试");
exit("没有获取到定时任务进程的权限 请刷新重试");
}
}
}else{
send_timed_task_log("定时任务配置文件不存在 不执行该");
exit("<p>定时任务配置文件不存在 不执行该任务</p>");
}
//用来发送定时任务日志的函数
function send_timed_task_log($str) {
//接收从脚本传递来的日志信息 判断当前的日期 把这个日志信息存储到当前日期命名的日志文件里
if (trim($str)<>""){
$nowtimefilepath="timed_task_".date("y-m-d",time()).".log";
$send_str=date("y-m-d",time());
$send_str=date("Y-m-d H:i:s",time())."----".trim($str)."rn";
file_put_contents($nowtimefilepath,$send_str,FILE_APPEND);//????? ???????
}else{
}
}
?>
</body>
</html>


感谢支持与鼓励~