0

Ok! So here is what I'm trying to accomplish... Lets call the file test1.php Using file_get_contents i'm fetching data from one site and inserting it into another database in a loop of 3 sec because it takes 1 sec to fetch and store so with another 1 sec buffer I'm hoping to get data that's no older than 5 secs. The file works on my localhost/test1.php just fine.. on my local I add a java script to refresh the browser... If i replace my $con settings to my server, and hit the file on my local browser using wamp that works too.. the problem begins when I upload it on my server and add it as a cron, hitting it just once in the morning and letting it run all day the loop works while the values it returns are null.

Things you may want to know...

1) My apache server mods enabled are the same as those on my wamp (curl installted, php 5.5.9 allow_url_fopen = On allow_url_include = Off)

2) Apache Mods

3) Code

<?php

$con=mysqli_connect("localhost","user","pwd","dbname");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }

$open_time=strtotime('07:00:00');
$close_time=strtotime('23:55:00');
$current_time=strtotime(date('H:i:s'));

/***************** End Timing ********************/


if($current_time >= $open_time && $current_time<=$close_time) {

while ($current_time < $close_time) {
// Perform queries 
$dataQuery = mysqli_query(); //my query goes here
while($row = $dataQuery->fetch_assoc())
{
    $data = @file_get_contents();

            $insert = mysqli_query(); //insert this data into my db


 }

 sleep(3);
    }

 mysqli_close($con);
 }
 ?>

4) The different ways I've tried to execute the cron :p

php /home/domain/public_html/test1.php php5 /home/domain/public_html/test1.php php /home/domain/public_html/test/test1.php /usr/bin/php -q /home/domain/public_html/test1.php /usr/bin/php -q /home/domain/fcgid/test1.php

5) The link to file get redirects to https (I'm still trying to see if its affecting anything... but I'm more inclined to rule this point out as the link works and stops randomly.)

6) After fixing all errors the file returns no output, which could also mean there are no errors (or they're emailed to me)

7) When it does work for a while, it will fetch values for a while, but stops after 2-3hrs, I can see its created several connections that are idle/sleep. It freezes mysql, and sometimes maxes out the server resources. Even rebooting doesn't help, values inserted after reboot are nulls and 0's

8) I'm still playing with max_execution_time .. I've currently set to 61,200 secs (17hrs) I'm also simultaneously running the same test on another sever with max_execution_time set to 60 and the cron hitting the file every 60 secs

9) Like i said before, if I hit the file locally on my windows system it works like its supposed (after switching $con details to my server)I add a javascript and open it in firefox/chrome localhost/test1.php

Help me please!

************My Questions************** 1) What am I doing wrong, and how would you recommend I go about doing this task. 2) What all do i need enabled/disabled on my apache/php/ 3) Is there a better way of doing the same thing?


3
  • Remove the @ before your file_get_content() and you might get a helpful error message. Commented Feb 22, 2015 at 13:54
  • Also, you need to post your actual code. With this pseudocode nobody can tell what's wrong. Commented Feb 22, 2015 at 13:55
  • I tried that with and without the @ didn't make any difference... Commented Feb 24, 2015 at 12:06

1 Answer 1

1

I didn't bother reading your entire post, since it is way too long. From what I can understand, your script runs fine while started manually, but doesn't work fine from CRON.

Running PHP scripts from CRON doesn't set the current working directory out of the box. Furthermore sometimes even PATH is not set so cron won't know where to look for your php binary. You have to setup CRON so it is aware of your paths, or execute cron command as follows:

cd /home/domain/public_html/ && /usr/bin/php test1.php
Sign up to request clarification or add additional context in comments.

2 Comments

This was helpful.. it worked! but now goes off at 11.30am i checked the timezone but that didn't cause it, no errors.. it wont fetch values, but it inserts 0's/nulls and continues to update the time stamp.. take a look at the code and see if you can find anything.. thanks!
I don't know, your code is not helpful. I'd suggest you to rewrite your question, simplify it, and post as a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.