Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9021645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:18:17+00:00 2026-06-16T05:18:17+00:00

I’m trying to create a cronjob in php by using a script with ignore_user_abort(true);

  • 0

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
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:18:19+00:00Added an answer on June 16, 2026 at 5:18 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.