需要创建定时任务,但不可能使用Cron作业(主机提供商有警告:“在45分钟内运行Cron作业多次是违反规则的,可能会导致关闭帐户。”)
应该每分钟执行php脚本(将数据从txt插入到mysql数据库),即应该调用此链接:http://www.myserver.com/ImportCumulusFile.php?type=dayfile&key=letmein&table=Dayfile&file=./data/Jan10log.txt
是否还有其他方法?
需要创建定时任务,但不可能使用Cron作业(主机提供商有警告:“在45分钟内运行Cron作业多次是违反规则的,可能会导致关闭帐户。”)
应该每分钟执行php脚本(将数据从txt插入到mysql数据库),即应该调用此链接:http://www.myserver.com/ImportCumulusFile.php?type=dayfile&key=letmein&table=Dayfile&file=./data/Jan10log.txt
是否还有其他方法?
使用像这样的外部网站在设定的时间间隔内触发您的URL。
使用meta刷新。更多信息请点击此处。您需要打开页面并保持它运行。
Javascript / Ajax刷新。与上面的示例类似。
设置cron作业。大多数共享主机都提供了设置cron作业的方式。查看您的托管cPanel。
如果你拥有 shell 访问权限,你可以通过 shell 执行一个 PHP 脚本。
像这样会是一个无限循环的例子,它会睡眠 60 秒执行,收集垃圾并重复直到永远。
while(true) {
sleep(60);
//script here
//end your script
}
你也可以使用ajax或meta refresh来实现“穷人版cron”。我以前做过这个。基本上,你只需要在脚本开头使用javascript或html的meta refresh添加一个重定向即可。从浏览器中访问此脚本,并保持打开状态。它将每60秒刷新一次,就像cronjob一样。
另一个替代cronjob的选择是bash脚本,如下所示:
#!/bin/bash
while :
do
sleep 60
wget http://127.0.0.1/path/to/cronjob.php -O Temp --delete-after
done
虽然如此说,你很可能会被主机发现并被终止。
因此,最好的解决方案:
去注册一个每月5-10美元的VPS,告别共享主机,拥抱运行自己的小服务器。
如果你这样做,甚至可以停止使用糟糕的PHP,改用Facebook的HHVM,并享受其出色的性能。
<?php
// crons.php
return [
'executebackup.php' => 1507979485,
'sendnewsletter.php' => 1507999485
];
?>
<?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);
?>
max_execution_time
。大多数情况下,它可以在无需通过访问者(如选项A)重新启动的情况下运行。// 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);
}
}
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);
// 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');
}
}
max_execution_time
足够高,您可以将其更改为每分钟仅调用一次(1440个请求/天)。