PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, November 14, 2022

[FIXED] How to circumvent the max_execution_time in PHP?

 November 14, 2022     cron, crontab, mailing, php     No comments   

Issue

I try to mailing messages to many subscribers. I need to execute my script for a long time, much more longer that is allowed with max_execution_time. I can use Cron Tab, which will be execute my script every time by schedule, but can I do it without cron tab? I try something like this:

    $maxTime = ini_get('max_execution_time');
    $startTime = time();
    foreach ($emails as $email) {
        if (time() < $startTime + $maxTime - 2) {
            // do something
        } else {
            // reload this page
        }
    }

And it's work well, but if I close this page in browser tab, it die and don't reload. I remind, that I'm looking for the implementation of this without Cron Tab. I want to start execution manually once and that to work it in the background later.

NOTE: Also I want to note that I don't consider the load on the server and send mail possible interval at this stage!


Solution

You have a few options:

Command Line Your best option is to run this on the command line manually. Command line scripts are much better for running long running processes.

Shell Exec If you must run this via browser, you can trigger the command line script by using exec eg shell_exec('php -f /var/www/domain.com/myLongRunningProcess.php > /dev/null 2>/dev/null &')

Ignore User Abort You can also run things after the browser has detached from a browser session. This is the most complex and hardest to debug but it will work. You must calculate the exact size of the page and then send the output and use the function ignore_user_abort so that your script will continue to run when the browser disengages.



Answered By - edmondscommerce
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing