Running a PHP file in CRON JOB - cPanel

Discussion in 'PHP' started by gch5185, Nov 11, 2010.

  1. #1
    Hi.

    I am trying to run a php file of my own in the cron job. The purpose of file is to fetch data from a website and stores the them into my database. Unfortunately, there is an issue with the execution.

    Basically, I'm trying to do it this way. I schedule the scheduler to run this file daily. I make my file to point to another page at the end of the execution by using the following code and it's working like a charm in my WAMP but not in cron job.

    echo "<script>document.location.href='get-data.php?page=" . ($_GET["page"] + 1) ."'</script>";
    echo "<script>'Content-type: application/octet-stream'</script>";
    Code (markup):
    In WAMP, the page will increment by 1 until the limit I've set. Too bad in cron, it only loads the first page and it stop without redirecting to another page. Can anyone point out my problem or any good solution for my problem? Thanks!
     
    gch5185, Nov 11, 2010 IP
  2. irving

    irving Peon

    Messages:
    65
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    When you're running this in cron you're not using a browser, but you seem to be calling javascript methods in your php file. That's not going to work in a file that is executed on the server outside of a browser. You would be better off making a loop in your php code.
     
    irving, Nov 11, 2010 IP
  3. gch5185

    gch5185 Peon

    Messages:
    114
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I tried making a loop in my php file.. I get an error.. sth like memory exhausted.. 356748839kb.. yeah sth like that..
     
    Last edited: Nov 11, 2010
    gch5185, Nov 11, 2010 IP
  4. irving

    irving Peon

    Messages:
    65
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Okay, but that means the problem is in your code. You say you have set a limit in your code with the javascript? Did you do this with your PHP code too? If you just keep incrementing the $GET['page'] number then either of your scripts will never finish - you need to make sure you have an escape clause in your loop, because at the moment you have recursion with no end state, just more and more calls to do the same thing.

    I see you set a limit in your javascript version, so perhaps you also had a limit in your pure PHP version.

    You also need to make sure your PHP memory allocation and execution time is long and large enough to run the script - read up on php.ini options for these things
     
    Last edited: Nov 11, 2010
    irving, Nov 11, 2010 IP
  5. gch5185

    gch5185 Peon

    Messages:
    114
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I know what is loop, by the way this is just the partial of the code. Thanks for the explanation, I just realized that cron is not a browser and does not support javascript but is there anyway for it to open the next page which is incremented by one? Just wondering.

    By the way this is the error message..

    Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 64 bytes) in ..
     
    Last edited: Nov 11, 2010
    gch5185, Nov 11, 2010 IP
  6. irving

    irving Peon

    Messages:
    65
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    If you wrote a looping php script without the javascript and set a reasonable-sized limit, then your only other problem would be the restrictions of your php.ini settings. Your script is using 32MB of memory at the moment so you would either need to increase the memory available (which is a setting in php.ini) or break your code down into smaller cycles - perhaps instead of setting it to run daily you could set it to run hourly and break the number of pages into 12? So if you had 1200 pages to retrieve, you would run your first file (1-1000) at 1am, the second (1001-2000) at 2am and so on.

    I would make sure first though that your code is not just infinitely looping by testing it with very small limits (say 5 or 10) - if you are still having problems there then the issue is somewhere in your loop and not with the number of pages you are trying to retrieve.
     
    irving, Nov 11, 2010 IP
  7. gch5185

    gch5185 Peon

    Messages:
    114
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Just to update you that I have found way to overcome this problem. I just set all my variables to NULL at the end of the loop with the idea to free the memory? (Am I doing the right thing?) Then I just realized that the maximum execution time is 60 seconds and I have changed it to 30mins by using this php code set_time_limit (1800); and everything should be fine now. Will need to see the next report which is tomorrow. By the way, thanks irving. :)

    By the irving, what are you a website developer like me too?
     
    gch5185, Nov 11, 2010 IP
  8. irving

    irving Peon

    Messages:
    65
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    irving, Nov 12, 2010 IP