简体   繁体   中英

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('/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!

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

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 need to make sure that the exec function is allowed in your php.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM