选项 A
实现它的一种简单方法是创建一个包含 php 脚本执行时间的文件/数据库条目:
<?php
// crons.php
return [
'executebackup.php' => 1507979485,
'sendnewsletter.php' => 1507999485
];
?>
并且在通过您的访问者提出的每个请求中,您检查当前时间,如果它更高,您包括您的 php 脚本:
<?php
// cronpixel.php
$crons = @include 'cache/crons.php';
foreach ($crons as $script => $time) {
if ($time < time()) {
// create lock to avoid race conditions
$lock = 'cache/' . md5($script) . '.lock';
if (file_exists($lock) || !mkdir($lock)) {
continue;
}
// start your php script
include($script);
// now update crons.php
$crons[ $script ] += 86400; // tomorrow
file_put_contents('cache/crons.php', '<?php return ' . var_export($crons, true) . '; ?' . '>')
// finally delete lock
rmdir($lock);
}
}
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// image data
$im = imagecreate(1, 1);
$blk = imagecolorallocate($im, 0, 0, 0);
imagecolortransparent($im, $blk);
// image output
header("Content-type: image/gif");
imagegif($im);
// free memory
imagedestroy($im);
?>
注意:它很少会在准确的第二秒被调用,因为您不知道访问者何时会打开您的页面(可能会在 2 秒后)。因此,设置第二天的新时间而不是通过添加 86400 秒是有意义的。而是使用mktime。
选项 B
This is a little project 我过去意识到,这与@r3wt 的想法相似,但涵盖了竞争条件,并且可以像 cronjob 在调度程序中那样在精确的时间上工作,而无需点击max_execution_time。而且它大部分时间都可以正常工作而无需重新启动它(就像选项 A 中的访问者所做的那样)。
说明:
该脚本在一分钟的第 15、30、45 和 60 秒写入锁定文件(以避免竞争条件):
// cron monitoring
foreach ($allowed_crons as $cron_second) {
$cron_filename = 'cache/' . $cron_second . '_crnsec_lock';
// start missing cron requests
if (!file_exists($cron_filename)) {
cron_request($cron_second);
}
// restart interrupted cron requests
else if (filemtime($cron_filename) + 90 < time()) {
rmdir($cron_filename);
cron_request($cron_second);
}
}
每次缺少锁定文件时,脚本都会创建它并使用sleep() 精确到秒:
if (file_exists($cron_filename) || !mkdir($cron_filename)) {
return;
}
// add one minute if necessary
$date = new DateTime();
$cron_date = new DateTime();
$cron_date->setTime($cron_date->format('H'), $cron_date->format('i'), $sec);
$diff = $date->diff($cron_date);
if ($diff->invert && $diff->s > 0) {
$cron_date->setTime($cron_date->format('H'), $cron_date->format('i') + 1, $sec);
}
$diff = $date->diff($cron_date);
// we use sleep() as time_sleep_until() starts one second to early (https://bugs.php.net/bug.php?id=69044)
sleep($diff->s);
再次唤醒后,通过fopen()向自己发送请求:
// note: filter_input returns the unchanged SERVER var (http://php.net/manual/de/function.filter-input.php#99124)
// note: filter_var is unsecure (http://www.d-mueller.de/blog/why-url-validation-with-filter_var-might-not-be-a-good-idea/)
$url = 'http' . isSecure() . '://' . filter_input(INPUT_SERVER, 'HTTP_HOST', FILTER_SANITIZE_URL) . htmlspecialchars($request_uri, ENT_QUOTES, 'UTF-8');
$context = stream_context_create(array(
'http' => array(
'timeout' => 1.0
)
));
// note: return "failed to open stream: HTTP request failed!" because timeout < time_sleep_until
if ($fp = @fopen($url, 'r', false, $context)) {
fclose($fp);
}
rmdir($cron_filename);
通过它无限调用自己,您可以定义不同的开始时间:
if (isset($_GET['cron_second'])) {
if ($cron_second === 0 && !(date('i') % 15)) {
mycron('every 15 minutes');
}
if ($cron_second === 0 && !(date('i') % 60)) {
mycron('every hour');
}
}
注意:它每天产生 5760 个请求(每分钟 4 个)。不多,但 cronjob 使用的资源要少得多。如果您的 max_execution_time 足够高,您可以将其更改为每分钟仅调用一次(1440 个请求/天)。