Jump to content

Execute PHP file from another PHP file


loki8

Recommended Posts

Hi all,

 

 

I have a script that runs for about 10 hours. (continuously)

 

I made a cron job for it and it was executed with no problem. But, after 25 minutes of successful execution I got a 500 Internal Server Error and the script stopped running.

 

Now, I will split this huge script into smaller ones. Each one of them will run for about 5 minutes so there shouldn't be any problem with the Cron Job.

 

The thing is, I don't know how to "call" a new PHP file to run/execute from another PHP file.

 

Is there any code for that? I am planning to place that code at the end of each file so it calls the next one.

 

Lets say I have 2 files, exec1.php and exec2.php

 

I will place the code at the end of exec1.php so it should call exec2.php

After that, the exec1.php will stop running right?

 

 

Waiting for a good a answer on that as I am stuck lol

 

 

(If you know what is causing the 500 Internal Server Error would be even cooler lol )

Link to comment
Share on other sites

Hi, thanks for the reply.

 

I am sending a Mass PM from my SMF forum but not from it lol  I know it sounds ridiculous but I have no way to split this cause I get the 500  Error when I send it from my forum interface too.  SO, creating a script that would do the same but sending them 10 by 10 is better and ensures delivery imo

 

Anyway, here is the code :

 


<?php
include('Settings.php');
include('SSI.php');
include('Sources/Subs-Post.php');


$title = '******************';
$message = '*********';



$recipients = array( 
       'to' => array(), 
      	 'bcc' => array(1,2,3,4,5,6,7,8,9,10,), 
       ); 
sendpm($recipients, $title, $message, $from = NULL); 
sleep(30); 


$recipients = array( 
       'to' => array(), 
      	 'bcc' => array(11,12,13,15,16,17,18,19,20,21,), 
       ); 
sendpm($recipients, $title, $message, $from = NULL); 
sleep(30); 


$recipients = array( 
       'to' => array(), 
      	 'bcc' => array(22,23,24,25,26,27,28,29,30,31,), 
       ); 
sendpm($recipients, $title, $message, $from = NULL); 
sleep(30); 


$recipients = array( 
       'to' => array(), 
      	 'bcc' => array(32,33,34,35,36,37,38,39,40,41,), 
       ); 
sendpm($recipients, $title, $message, $from = NULL); 
sleep(30); 


//AND SO ON TILL THIS : 


$recipients = array( 
       'to' => array(), 
      	 'bcc' => array(13254,13255,13256,13257,13258,13259,13260,13261,13262,13263,), 
       ); 
sendpm($recipients, $title, $message, $from = NULL); 
sleep(30); 


$recipients = array( 
       'to' => array(), 
      	 'bcc' => array(13264,), 
       ); 
sendpm($recipients, $title, $message, $from = NULL); 
sleep(30); 


$to      = '*****@****.com';
  $subject = 'Mass PM has Finished!!!';

  $message = "Mass PM has Finished!!!";
            
  $message = urldecode(wordwrap($message, 70));

  $headers = 'From: Steve the Server <server@*******.com>' . "\r\n" .
    'Reply-To: server@********.com. "\r\n" .
    'X-Mailer: PHP/' . phpversion();

  mail($to, $subject, $message, $headers);
?>

 

 

 

 

*My forum has 13000+, that's why I can not send it from the regular forum interface.

Link to comment
Share on other sites

Ok, I think you misunderstood what I am doing.

 

If I run this file ONCE, it will run for 10 hours and then finish(in theory)

 

Because of the error I get on the cron job, I can't run it at once and I need to split it.

 

 

I use a Cron Job because I don't want to have the browser open for 10 hours when running it. Running in the background is better I think lol

 

Lastly, I get the 500 error even when I make a Mass PM from the forum AND when I run this script from my browser.

Link to comment
Share on other sites

Yes, I have tried to run and it runs with no problem

 

Also, the sleep is needed cause I want the server not to choke. I had tried to send many PMs at once but my server couldn't handle it.

 

btw, the code you gave me... Will it close the one file and execute the other?  Cause if it executed from the same initial file I will get the 500 Internal error after 25 minutes again.

 

 

let me know

Link to comment
Share on other sites

 

the code i gave you would flush then first script the continue with the second.

 

this would run the second scripts 5secs after the first one using cron.

php -q /address/cron.php; sleep 5; php -q /address/cron.php

maybe you would like to split your email saying that exec1 - done. then exec2 - done.

Link to comment
Share on other sites

What I did was split the initial file to smaller ones. Each one of them takes hardly 6 minutes to finish.

So, I now have 131 files to run.

 

I also used the techinque mentioned here : http://www.steveroot.co.uk/2010/07/cron---multiple.html

This way, on 1 Cron Job I was able to add 131 executions of the different files.

 

(I am using sleep(30); at the end of every file as well as between every PM sent as the code above indicates)

 

I was getting the 500 Internal Server Error within 25 minutes with the old way.

Now, it's been about 1 hour of successful execution of every file and still continuing as I write this. Let's see how it goes :D

 

THANKS everyone ;)

Link to comment
Share on other sites

131 files?

 

Your server is going to get angry and steal all your Christmas presents. Can you explain in detail what exactly you are trying to do or perhaps post some code from one of those files? I am guessing you are making this way harder than it needs to be.

Link to comment
Share on other sites

I am sending a Mass PM to my SMF forum. It has 13000+ members.

 

If I send all PMs at once, MANY aren't sent at all and most important after the first 100 members NO Email notification is sent at all lol

 

Here is the file number 17 :

 

<?php
include('Settings.php');
include('SSI.php');
include('Subs-Post.php');
include('pm.php');

$recipients = array( 
       'to' => array(), 
      	 'bcc' => array(1811,1812,1813,1814,1815,1816,1817,1818,1819,1820,), 
       ); 
sendpm($recipients, $title, $message, $from = NULL); 
sleep(30); 


$recipients = array( 
       'to' => array(), 
      	 'bcc' => array(1821,1822,1823,1824,1825,1826,1827,1828,1829,1830,), 
       ); 
sendpm($recipients, $title, $message, $from = NULL); 
sleep(30); 


$recipients = array( 
       'to' => array(), 
      	 'bcc' => array(1831,1832,1834,1835,1836,1837,1839,1840,1841,1842,), 
       ); 
sendpm($recipients, $title, $message, $from = NULL); 
sleep(30); 


$recipients = array( 
       'to' => array(), 
      	 'bcc' => array(1843,1844,1845,1846,1847,1848,1849,1850,1851,1853,), 
       ); 
sendpm($recipients, $title, $message, $from = NULL); 
sleep(30); 


$recipients = array( 
       'to' => array(), 
      	 'bcc' => array(1854,1856,1857,1858,1859,1860,1861,1862,1863,1864,), 
       ); 
sendpm($recipients, $title, $message, $from = NULL); 
sleep(30); 


$recipients = array( 
       'to' => array(), 
      	 'bcc' => array(1865,1866,1867,1868,1869,1870,1871,1872,1873,1874,), 
       ); 
sendpm($recipients, $title, $message, $from = NULL); 
sleep(30); 


$recipients = array( 
       'to' => array(), 
      	 'bcc' => array(1876,1877,1878,1879,1880,1881,1882,1883,1884,1885,), 
       ); 
sendpm($recipients, $title, $message, $from = NULL); 
sleep(30); 


$recipients = array( 
       'to' => array(), 
      	 'bcc' => array(1886,1887,1888,1890,1891,1892,1893,1894,1895,1896,), 
       ); 
sendpm($recipients, $title, $message, $from = NULL); 
sleep(30); 


$recipients = array( 
       'to' => array(), 
      	 'bcc' => array(1899,1900,1901,1902,1903,1904,1905,1906,1907,1908,), 
       ); 
sendpm($recipients, $title, $message, $from = NULL); 
sleep(30); 


$recipients = array( 
       'to' => array(), 
      	 'bcc' => array(1909,1910,1911,1912,1913,1914,1915,1916,1917,1918,), 
       ); 
sendpm($recipients, $title, $message, $from = NULL); 
sleep(30); 


?>



 

Same thing happens on the rest 130. Every file is executed for about 6 minutes from which the 5 are for  the sleep() function ( I use sleep(30) 10 times )

 

And here is the Cron Job. (It is all 1 :P )

 

curl http://www.dailysurveys4u.com/forum/sendpm/001.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/002.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/003.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/004.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/005.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/006.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/007.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/008.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/009.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/010.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/011.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/012.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/013.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/014.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/015.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/016.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/017.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/018.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/019.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/020.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/021.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/022.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/023.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/024.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/025.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/026.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/027.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/028.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/029.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/030.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/031.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/032.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/033.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/034.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/035.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/036.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/037.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/038.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/039.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/040.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/041.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/042.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/043.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/044.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/045.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/046.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/047.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/048.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/049.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/050.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/051.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/052.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/053.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/054.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/055.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/056.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/057.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/058.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/059.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/060.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/061.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/062.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/063.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/064.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/065.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/066.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/067.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/068.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/069.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/070.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/071.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/072.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/073.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/074.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/075.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/076.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/077.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/078.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/079.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/080.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/081.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/082.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/083.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/084.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/085.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/086.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/087.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/088.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/089.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/090.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/091.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/092.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/093.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/094.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/095.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/096.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/097.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/098.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/099.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/100.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/101.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/102.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/103.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/104.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/105.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/106.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/107.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/108.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/109.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/110.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/111.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/112.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/113.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/114.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/115.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/116.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/117.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/118.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/119.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/120.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/121.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/122.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/123.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/124.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/125.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/126.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/127.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/128.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/129.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/130.php >/dev/null 2>&1 && curl http://www.dailysurveys4u.com/forum/sendpm/131.php >/dev/null 2>&1

 

Every file is run ONLY if the previous has finished. This way multiple file-runs are avoided.

 

 

Btw, I just checked RAM Usage in my site. It is currently at 481 MB .

A normal day it doesn't exceed 476 MB

 

When I send a Mass PM from SMF it sky-rockets to 650 MB causing some trouble lol (let alone the fact that it doesn't work as it should. not sending PMs, email notifications etc)

 

cool huh? I have to say it works for me :)

 

 

*Every file wasn't created manually of course

 

 

 

Forgot to mention that if I try to send a Mass PM from SMF, after around half an hour I get a "500 Internal Server Error" when using both browser and a Cron Job.

 

Splitting them does the job...

 

I can't hold my self from asking, is there any other work around for this?

 

 

 

EDIT : Everything is working smoothly since started running this about 1 hour and a half ago. :)

Link to comment
Share on other sites

How are you sending the emails? I'm assuming the mail() function.

 

The mail function wasn't really meant to send messages in that manner. http://stackoverflow.com/questions/1118154/sending-mass-email-using-php

 

From http://php.net/manual/en/function.mail.php

It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient.

 

For the sending of large amounts of email, see the » PEAR::Mail, and » PEAR::Mail_Queue packages.

 

You don't need all those files to do that. You could run a cron on the same file. The file could execute a certain number of queries and stop itself. Or you could make a database that handles mass pm's sort of like stickies on a forum. That would save a LOT of server load.

Link to comment
Share on other sites

I am sending Personal Messages to my forum using the sendpm() function from SMF

Along with the PM, an email notification is sent to the member that received the PM. I do NOT control the email notifications.

 

 

The file could execute a certain number of queries and stop itself. Or you could make a database that handles mass pm's sort of like stickies on a forum

 

I do not know how to do that. If I knew how place query results within an array and execute them 10 by 10 I would have done it, no doubt

Link to comment
Share on other sites

<?php
   $start=file_get_contents("startPoint.txt");
   $sql="SELECT * FROM user_table LIMIT $start, 10";
   if ($query=@mysql_query($sql)) {
      if (@mysql_num_rows($query) > 0) {
         while ($req=@mysql_fetch_array($query)) {
            sendpm($req['recipient'], $title, $message $from);
            $start++;
         }
      }
   }
   file_put_contents("startPoint.txt", $start);
?>

 

This is a pretty basic example of how you might do this.

 

If you are sending a lot of emails, you should mass as many of them in one "to" as possible. Like the PHP manual says, mail() opens a socket each time. So it's better to keep the socket open and send as many as possible to save resources. However, once again, you should look into that pear module. If you use mail, you are bogging down your server and you should be even more careful because you could get your mailserver / web address blacklisted.

 

Edit: For some reason, the page submitted it while I was still typing it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.