一个Cron作业运行多个PHP页面

时间:2011-04-30 13:43:38

标签: php cron

我想在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';
?>

谢谢!

4 个答案:

答案 0 :(得分:0)

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

wget -i CronScripts.txt

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

答案 1 :(得分:0)

您是否有可能在safe_mode中运行并且不允许修改max_execution_time?

答案 2 :(得分:0)

你可以使用这种技术,它可以帮助调用你喜欢的页面,所有页面将一次独立运行,而不必等待每个页面响应异步。

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

答案 3 :(得分:-1)

你需要确保你的php中允许exec函数。