Reputation: 14173
I'm trying to create a cronjob in php by using a script with ignore_user_abort(true);
and set_time_limit(0);
. The basic works, but unfortunately the process is killed anyway after 15mins by the server.
Now i'm using fopen
to bypass this by requesting the page again before its killed. This works as expected the first time (it truncates the table, loops and then loads page with id=2). A second row with id=2 is inserted and the loop starts correct. Unfortunately after page 2 it keeps using the same Id, it does not continue with id=3, 4, etc.
Anybody has an idea on how to fix this?
Sample code i use for testing:
<?php
ignore_user_abort(true);
set_time_limit(0);
$interval=1;
$startTime = time();
$maxLoop = 10;
$id = (strlen($_GET['id'])>0) ? trim($_GET['id']) : 1;
require_once('ct2database.php');
ct2database::init();
if ($id==1) {
//init table
ct2database::query("TRUNCATE TABLE crontest");
}
ct2database::query("INSERT INTO crontest (Id, Counter, StartDate) VALUES (".$id.", 0, '".date("Y-m-d H:i:s")."')");
//loop
do{
ct2database::query("UPDATE crontest SET Counter=Counter+1 WHERE id=".$id);
sleep($interval);
}while($maxLoop--);
$newid = $id+1;
$url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].'?id='.$newid . '&t='.time();
//regular end of loop
ct2database::query("UPDATE crontest SET EndDateRegular='".date("Y-m-d H:i:s")."' WHERE id=".$id);
$context = stream_context_create( array(
'http'=>array(
'timeout' => 0.5
)
));
$fp = fopen($url, 'r', false, $context);
register_shutdown_function('ShutdownHandler');
function ShutdownHandler() {
global $id;
//update on shutdown
ct2database::query("UPDATE crontest SET EndDateShutdown='".date("Y-m-d H:i:s")."' WHERE id=".$id);
}
set_error_handler("ErrorHandler");
function ErrorHandler() {
global $id;
//update on error
ct2database::query("UPDATE crontest SET EndDateError='".date("Y-m-d H:i:s")."' WHERE id=".$id);
}
?>
and a result:
Array
(
[Id] => 1
[Counter] => 10
[DateModified] => 2012-12-21 16:01:54
[StartDate] => 2012-12-21 16:01:42
[EndDateRegular] => 2012-12-21 16:01:53
[EndDateShutdown] => 2012-12-21 16:01:54
[EndDateError] => 0000-00-00 00:00:00
)
Array
(
[Id] => 2
[Counter] => 55
[DateModified] => 2012-12-21 16:02:49
[StartDate] => 2012-12-21 16:01:54
[EndDateRegular] => 2012-12-21 16:02:49
[EndDateShutdown] => 2012-12-21 16:02:49
[EndDateError] => 0000-00-00 00:00:00
)
Upvotes: 0
Views: 394
Reputation: 74098
I tried your script "restart" with this small example and it worked fine so far.
<?php
if (isset($_GET['id'])) {
$id = $_GET['id'];
} else {
$id = 1;
}
echo "$id ";
++$id;
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] . '?id=' . $id;
if ($id < 10) {
$fp = fopen($url, 'r');
$buf = fread($fp, 100);
echo $buf;
fclose($fp);
}
However, I used PHP_SELF
, because REQUEST_URI
includes the complete path including parameter id
. This builds up to urls like http://server/path?id=1?id=2?id=3?id=...
. I also read the output from the script, otherwise it might block, when some output occurs.
Another point to keep in mind is, that this is effectively an endless recursion. This might result in connection refused
errors at some time.
So the real solution should be to look into the server logs and find out, what's real reason for your problem.
Upvotes: 1