emeraldhieu
emeraldhieu

Reputation: 9439

PHP scripts stops extraordinarily in crontab

I set a cron to run this script. I used recursion to update a table (250 rows every time). It runs well at the beginning but after some minutes, it stops without printing "Finish". It seems to be a memory problem of server. I checked "Processes" in PhpMyadmin and make sure it runs well but it stops after some minutes. How to fix it?

function update_mt_image_inventory($db, $from)
{
    $current_img_id = htmlentities($_SERVER['argv'][1], ENT_QUOTES);  
    if(!$current_img_id) $current_img_id = 999999999;   
    $interier = 250;
    $sql = "SELECT im.IMG_ID, im.IMG_FILENAME, im.SEAL_NUMBER , im.XXX FROM `mt_images` im WHERE im.IMG_ID < {$current_img_id} AND im.ACT_FLG = 1 ORDER BY im.IMG_ID DESC LIMIT {$from}, {$interier}";
    $re_check = @$db->sql_query($sql);
    if(@mysql_num_rows($re_check) > 0) {
        while($row = $db->sql_fetchrow($re_check)) {
            $img_id =  $row['IMG_ID'];
            $current_img_id = $img_id;:q!
            $inventory =  $row['XXX'];

            $sql = "SELECT USER_ID FROM tr_lines_alls WHERE IMG_ID = {$img_id} AND ACT_FLG = 1";
            $img_res = @$db->sql_query($sql);
            $img_count = @mysql_num_rows($img_res) ;
            if($img_count != $inventory && $img_id>0){
                $sql = "UPDATE mt_images SET XXX = {$img_count} WHERE IMG_ID = {$img_id}";
                @$db->sql_query($sql);
                $data = "{$img_id} | {$inventory} | {$img_count}";
                echo $data."\n";
                if($img_count>0) writeLog($data);
            }
            @mysql_free_result($img_res);
            unset($img_res);
        }
        @mysql_free_result($re_check);
        unset($re_check);
        echo "Checking next {$interier}/{$from} - Current: {$current_img_id} - Memory: ".memory_get_usage(false)."\n"; 
        if($from%5000 ==0){
            echo date("h:i:s")." --> 5000 records passed.\n";
        }
        update_mt_image_inventory($db, $from + $interier);
    } else {
        writeLog("mt_images finished");
    }
}

Upvotes: 0

Views: 237

Answers (1)

Andy Pieters
Andy Pieters

Reputation: 343

Could be the time limit setting. Try this on the first line of your code:

set_time_limit(0);

Upvotes: 2

Related Questions