-
Notifications
You must be signed in to change notification settings - Fork 4
/
xcron.php
71 lines (69 loc) · 1.21 KB
/
xcron.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
// 设定参数
$phpfile = 'cron.php';// 需要执行的Cron文件名
$time = 1;// 间隔分钟数
// 关闭浏览器仍然执行
set_time_limit(0);
ignore_user_abort(true);
// 读取记录
$A=$B=$C=$D= 0;
$F = off;
include('cronlog.php');
if (time() - $A < 30)
exit;
// 判断是否有进程在执行
if ($F == on)
exit();
// 判断D是否为空
if ($D == 0){
$D = $C;
$C = $B;
$B = $A;
$A = time();
writelog($A,$B,$C,$D,$F);
include($phpfile);
exit();
}
// 启动修正
$D = $C;
$C = $B;
$B = $A;
$A = time();
$E = ($A-$D)/3;
writelog($A,$B,$C,$D,$F);
// 计算运行次数
$time *= 60;
$i=round($E/$time);
if ($i <= 0){
include($phpfile);
exit();
}
if ($i > 60){
$A=$B=$C=$D= 0;
$F = off;
writelog($A,$B,$C,$D,$F);
exit();
}
// 防止多进程运行
$F = on;
writelog($A,$B,$C,$D,$F);
// 循环
$u=1;
while($u<=$i){
include($phpfile);
if ($A+$E-time()<120){
$F = off;
writelog($A,$B,$C,$D,$F);
}
if ($A+$E-time()<60)
exit();
sleep ($time);
$u++;
}
exit();
// 自定义函数
function writelog($A,$B,$C,$D,$F){
$file = '<?php'.PHP_EOL.'$A = '.$A.';'.PHP_EOL.'$B = '.$B.';'.PHP_EOL.'$C = '.$C.';'.PHP_EOL.'$D = '.$D.';'.PHP_EOL.'$F = '.$F.';'.PHP_EOL.'?>';
file_put_contents("cronlog.php",$file);
}
?>