开发者

One Cron Job Running Multiple PHP pages

I would like to set a cron job in cpanel to run these different pages. I thought it would be easier to put them in one file. I know how to set these up to run individually, but the way it is written below, it won't run.

What do I need to change to get it to run smoothly?

<?php
 ini_set('max_execution_time', 18000);
 exec('/usr/bin/php -q /home2/sample/public_html/linktest/myapp-page1.php');  
sleep (120);
 exec('开发者_如何学Python/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';
?>

Thanks!


Or you could use wget and load a set of URLs from a file

wget -i CronScripts.txt

These would have to be accessible from the outside world though


Is it possible you are running in safe_mode and not allowed to modify max_execution_time?


you can use this technique it will help to call as many as pages you like all pages will run at once independently without waiting for each page response as asynchronous.

cornjobpage.php //mainpage

    <?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:if you want to send url parameters as loop then follow this answer :https://stackoverflow.com/a/41225209/6295712


you need to make sure that the exec function is allowed in your php.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜