質問

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!

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top