dongmeng2509 2017-07-24 16:14
浏览 38
已采纳

Crontab没有运行重载页面的javascript

I have a php file that send emails to our clients who are expecting it and to ease the load on the server and to eliminate it looking like a spam server we reduce the flow to 40 emails every 20 seconds. This is done by using javascript to rerun the page every 20 seconds. The page itself reads from a data base and gets the next 40 email addresses, sends them, and then recycles until all are sent. Then the email list is reloaded from a duplicate table for the next cycle three weeks out.

Here is the javascript code that seems to not run when initiated by a cron job

<script language="JavaScript" type="text/javascript">
            setTimeout("location.href = \'PT_enrollment.php?sendDate='.$sendDate.'\'",20000); // milliseconds, so 10 seconds = 10000ms
            </script>

When I run this from my browser it does run correctly so I guess it is in the Cron Job functionality.

What am I missing? Is there a setting or parameter I need to set in the crontab or on the server so it runs correctly?

  • 写回答

2条回答 默认 最新

  • dousu8767 2017-07-24 19:10
    关注

    You could just have your PHP process sleep for those 20 seconds:

    $emails = // retrieve all emails from database
    $done = 0;
    
    foreach ($emails as $email) {
        // send email to $email
    
        $done++;
        if ($done % 40 == 0) { // every 40 emails
            sleep(20); // wait 20 seconds
        }
    }
    

    Ordinarily, cron jobs are run server-side via php-cli, which by default doesn't have a timeout. Some hosting providers run cron jobs by actually retrieving a URL via wget or curl, in which case you do have to account for a possible timeout. In that case, you can add set_time_limit(0); to your script to work around that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?