【问题标题】:One Cron Job Running Multiple PHP pages一项运行多个 PHP 页面的 Cron 作业
【发布时间】:2011-04-30 13:43:37
【问题描述】:

我想在 cpanel 中设置一个 cron 作业来运行这些不同的页面。我认为将它们放在一个文件中会更容易。我知道如何将它们设置为单独运行,但是按照下面的写法,它不会运行。

我需要进行哪些更改才能使其顺利运行?

<?php
 ini_set('max_execution_time', 18000);
 exec('/usr/bin/php -q /home2/sample/public_html/linktest/myapp-page1.php');  
sleep (120);
 exec('/usr/bin/php -q /home2/sample/public_html/linktest/myapp-page2.php');  
sleep (120);
 exec('/usr/bin/php -q /home2/sample/public_html/linktest/myapp-page3.php');  
sleep (120);
 exec('/usr/bin/php -q /home2/sample/public_html/linktest/myapp-page4.php');  
sleep (120);
 exec('/usr/bin/php -q /home2/sample/public_html/linktest/myapp-page5.php');  
sleep (120);
echo 'Cron ran successfully';
?>

谢谢!

【问题讨论】:

  • 创建五个 crontab 条目有什么区别?

标签: php cron


【解决方案1】:

或者您可以使用 wget 并从文件中加载一组 URL

wget -i CronScripts.txt

这些必须可以从外部世界访问

【讨论】:

    【解决方案2】:

    您是否可能在安全模式下运行并且不允许修改 max_execution_time?

    【讨论】:

      【解决方案3】:

      您可以使用这种技术,它有助于调用任意数量的页面,所有页面将同时独立运行,而无需等待每个页面响应异步。

      cornjobpage.php //主页面

          <?php
      
      post_async("http://localhost/projectname/testpage.php", "Keywordname=testValue");
      //post_async("http://localhost/projectname/testpage.php", "Keywordname=testValue2");
      //post_async("http://localhost/projectname/otherpage.php", "Keywordname=anyValue");
      //call as many as pages you like all pages will run at once independently without waiting for each page response as asynchronous.
                  ?>
                  <?php
      
                  /*
                   * Executes a PHP page asynchronously so the current page does not have to wait for it to     finish running.
                   *  
                   */
                  function post_async($url,$params)
                  {
      
                      $post_string = $params;
      
                      $parts=parse_url($url);
      
                      $fp = fsockopen($parts['host'],
                          isset($parts['port'])?$parts['port']:80,
                          $errno, $errstr, 30);
      
                      $out = "GET ".$parts['path']."?$post_string"." HTTP/1.1\r\n";//you can use POST instead of GET if you like
                      $out.= "Host: ".$parts['host']."\r\n";
                      $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
                      $out.= "Content-Length: ".strlen($post_string)."\r\n";
                      $out.= "Connection: Close\r\n\r\n";
                      fwrite($fp, $out);
                      fclose($fp);
                  }
                  ?>
      

      testpage.php

          <?
          echo $_REQUEST["Keywordname"];//case1 Output > testValue
          ?>
      

      PS:如果您想将 url 参数作为循环发送,请按照以下答案操作:https://stackoverflow.com/a/41225209/6295712

      【讨论】:

        【解决方案4】:

        您需要确保 exec 函数在您的 php 中是允许的。

        【讨论】:

        • 始终允许执行;安全模式的限制是您可能只能在 safe_mode_exec 目录中运行脚本。但是如果开启了安全模式,他们就不能修改 max_execution_time。
        猜你喜欢
        • 2011-11-07
        • 2018-05-06
        • 2023-03-28
        • 2010-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-23
        相关资源
        最近更新 更多