Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP script stops running with 503 service unavailable

Tags:

php

execution

I have my own PHP script to send a newsletter. It worked perfectly. Recently, my hosting provider points me out a quota to respect: 30 mails/minute. So, between each mail sending, I insert sleep(2);.

Since that change, after 5 minutes (300 seconds), the script stops and return:

Service Unavailable

The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later. Additionally, a 503 Service Unavailable error was encountered while trying to use an ErrorDocument to handle the request.

Four observations.

  1. At the beginning of my script, I put those two lines:

    ignore_user_abort(true);
    set_time_limit(0);
    

    So, the problem comes not from the max_execution_time variable (I verify: it is indeed modified).

  2. The script works correctly on localhost, where the PHP ini variables are more limited than on my web host. So, the problem comes not from the memory_limit variable.

  3. I thought it could be the Apache Timeout variable limit (300). But my host provider is skeptical about it...

  4. The server works on PHP 5.5.

What could be causing this?

The script before (it works with PHPMailer) :

// all email addresses
while ($row = $result->fetch_array())
{
    // ...

    if ($mail->send())
    {
        echo "success : " . $row["mail"] . "<br/>";
    }
    else
    {
        echo "error : " . $row["mail"] . " - " . $mail->ErrorInfo . "<br/>";
    }
}

After :

// all email addresses
while ($row = $result->fetch_array())
{
    ...

    if ($mail->send())
    {
        echo "success : " . $row["mail"] . "<br/>";
    }
    else
    {
        echo "error : " . $row["mail"] . " - " . $mail->ErrorInfo . "<br/>";
    }

    sleep(2);
}
like image 642
Jiphou Avatar asked Oct 29 '25 19:10

Jiphou


1 Answers

I think on the server mod_fastcgi or equals are used.

Have a look at this old post http://www.binarytides.com/php-set-time-limit/

For example mod_fastcgi has an option called "-idle-timeout" which controls the idle time of the script. So if the script does not output anything to the fastcgi handler for that many seconds then fastcgi would terminate it. The setup is somewhat like this:

Apache <-> mod_fastcgi <-> php processes

You should use a queue and an crontab, which the queue processed once per minute.

Hope this helps.

like image 145
rengaw83 Avatar answered Oct 31 '25 09:10

rengaw83



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!