SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    May 2005
    Location
    London, ON
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Confused about timeouts and cron jobs

    Hello,

    I've been struggling with this lately. I have set up a few cron jobs to run some fairly simple PHP scripts at certain times.

    Each of these scripts, when its done running, is supposed to insert a row into a table of my sql database with a timestamp (so I can know that it ran successfully). Unfortunately, this has not been working.

    When I try to run the scripts manually, I get what seems to be a timeout to me. In firefox, it seems to run for a few seconds and then stops and pop-up shows up as if I was trying to download the .php file.

    Now, these scripts each contain a loop. If I put a simple: echo ". "; statement at the beginning of the loop, the script completes without error when I execute it manually in the browser. This makes me think its a broweser thing (it needs output within a certain amount of time?). However, it still doesn't seem to complete when run as a cron job with or without the echo statement.

    Can anyone provide insight on what is happening? This is driving me nuts
    Ryan Price
    Subscribe to my blog for regular tips and tricks

  2. #2
    SitePoint Evangelist optl's Avatar
    Join Date
    Oct 2004
    Location
    Washington DC
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Post the script.
    For the phrase "Bethesda home architect", my clients
    websites occupy 6 of the first 8 results
    on the 1st page of Google. My Secret SEO Strategy Revealed

  3. #3
    SitePoint Addict
    Join Date
    May 2005
    Location
    London, ON
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The script:
    PHP Code:
    <?php

    require_once("lib.php");
    update_rankings();

    $query "INSERT INTO cronJobUpdates (type) VALUES ('rankings')";
    mysql_query($query);
    ?>
    and the function inside lib.php it calls:

    PHP Code:
    //will update the rankings in the DB
    function update_rankings() {

        
    //get an array of all the portfolios as Portfolio objects
        
    $portfolios get_portfolios();

        if (isset(
    $portfolios)) {
        
            
    //delete the old rankings and save the new ones
            
    $query "DELETE FROM rankings WHERE 1=1";
            
    mysql_query($query) or die("Error deleting old rankings: ".mysql_error());
            
            
    //for each one of the portfolios...
            
    foreach ($portfolios as $port) {
                
    //give the script more time to finish
                
    set_time_limit(30);
                
                
    $info $port->get_info();
                
    $totals $port->get_totals($info);
                
    $netWorth $totals['mktValue'] + $port->cash;
            
                
    $query "INSERT INTO rankings (uid, comp_id, netWorth) 
                          VALUES ('"
    .$port->uid."', '".$port->comp->id."', '".$netWorth."')";
                
    mysql_query($query) or die ("Insert failed: ".mysql_error());
            }
        }


    Ryan Price
    Subscribe to my blog for regular tips and tricks

  4. #4
    SitePoint Enthusiast mrsmiley's Avatar
    Join Date
    Jul 2004
    Location
    Melbourne
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First thing I'd try is to turn on all error reporting as the first line of the script:

    Code:
    error_reporting(E_ALL);
    Second thing to note is that there is a big difference between running a script within the browser, as opposed to a cron job. Depending on the environment, the cron job might run as a different user, and almost surely, it will use a different interpreter. ie the PHP CLI, instead of CGI or SAPI.

    You will need to add execute permission to your script and also add the #!/usr/bin/php equivalent to the first line of the script to associate it with the CLI.

    There are ways of scheduling cron jobs to use it via the web server instead, but they can be kludgey sometimes.

    BTW, most cron jobs have any output redirected to /dev/null meaning it disappears into the ether, so echo wont have any effect unless you explicitly catch it in the cron command and redirect the output to an email, file or some equivalent.

  5. #5
    SitePoint Addict
    Join Date
    May 2005
    Location
    London, ON
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @mrsmiley: thanks. I've been getting some help most of the night trying to set this up. Here is where I am now:

    I went in through shell to try and see what was happening when my jobs were executed. It seems that part of the problem is I am wanting to run php5, and the cron command is sending it through php4 by default (I've used an .htaccess file on my directory to run the files through php5 when its called from a browser).

    After much looking, I couldn't find the path to the php5 binary. I've emailed my host to ask. If they do provide it to me what would I change to get it to work?

    For the time being I've been running the files using curl: curl http://www.mysite.com/myFile.php

    However, I was still getting timeout issues unless I'm echoing something fairly regularly while the file loops.

    Any more guidance would be greatly appreciated. I'm trying to learn all I can.
    Ryan Price
    Subscribe to my blog for regular tips and tricks

  6. #6
    SitePoint Addict
    Join Date
    Jan 2007
    Location
    Romania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What if you use Lynx to run the script you want? Is just like opening yourself in browser.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •