Cron Job problem on Centos (Zpanel)

January 31, 2015 3.1k views

Hello,

Long time i trying to setup my gametracker web site,but whitout success.
This script is simular to gametracker.com or gametracker.rs
Here is a link: http://client.dizajnarena.com/index.php?p=server.list

The problem is: Cron Job don't work.

Is not script problem,beacuse some sites are used same script and its work.

Here is cron php code: cron.gametracker.php

<?php if(!isset($access_security) || $current_ip != $_SERVER['SERVER_ADDR']){die("direktan pristup zabranjen");}

$server_query   = mysql_query("SELECT id,ip,port,game FROM serverinfo WHERE UNIX_TIMESTAMP()-last_update>180 ORDER BY rank_pts DESC");
while($server_row = mysql_fetch_assoc($server_query)){
    $server_id                  = $server_row['id'];
    $server_ip                  = $server_row['ip'];
    $server_port                = $server_row['port'];
    $server_game                = $server_row['game'];

    require_once("gameq.php");
    $servers = array('server_'.$server_id => array($server_game, $server_ip, $server_port));
    $gq = new GameQ();
    $gq->addServers($servers);
    $gq->setOption('timeout', 200);
    $gq->setFilter('normalise');
    $gq->setFilter('sortplayers');
    $data = $gq->requestData();


    if($data['server_'.$server_id]['gq_online'] == "1"){
        $hostname           = mysql_real_escape_string($data['server_'.$server_id]['gq_hostname']);
        $mapname            = mysql_real_escape_string($data['server_'.$server_id]['gq_mapname']);
        $num_players        = mysql_real_escape_string($data['server_'.$server_id]['gq_numplayers']);
        $max_players        = mysql_real_escape_string($data['server_'.$server_id]['gq_maxplayers']);
        $password           = mysql_real_escape_string($data['server_'.$server_id]['gq_password']);
        $players            = $data['server_'.$server_id]['players'];

        $update_query       = mysql_query("UPDATE serverinfo SET online='1', hostname='$hostname', mapname='$mapname', num_players='$num_players', max_players='$max_players', password='$password', last_update='$timestamp' WHERE id='$server_id'");
        $del_player_query   = mysql_query("DELETE FROM players WHERE sid='$server_id'");
        foreach ($players as $player) {
            $player_nickname        = mysql_real_escape_string($player['gq_name']);
            $player_score           = mysql_real_escape_string($player['gq_score']);
            $player_time            = mysql_real_escape_string($player['time']);
            $player_nickname        = (!empty($player_nickname)) ? $player_nickname : 'anonymous';
            if(!is_numeric($player_time)){$player_time = '/';}
            $insert_player_query    = mysql_query("INSERT INTO players (id,nickname,score,time_online,mapname,sid) VALUES ('','$player_nickname','$player_score','$player_time','$mapname','$server_id')");
        }
    } else {
        $update_query       = mysql_query("UPDATE serverinfo SET online='0' WHERE id='$server_id'");
    }
}

This is Serbian "direktan pristup zabranjen" and its mean "direct access denied"

  • Also here is other cron file for rank game servers: cron.serverrank.php
<?php if(!isset($access_security) || $current_ip != $_SERVER['SERVER_ADDR']){die("direktan pristup zabranjen");}

$server_rank = 1;
// rank_pts+0 = order fix
$server_query   = mysql_query("SELECT id FROM serverinfo ORDER BY rank_pts+0 DESC");
while($server_row = mysql_fetch_assoc($server_query)){
    $server_id      = $server_row['id'];
    $update_query   = mysql_query("UPDATE serverinfo SET rank='$server_rank' WHERE id='$server_id'");
    $server_rank++;
}

$log_guery = mysql_query("INSERT INTO admin_log (id,text,url,uid,ip,created) VALUES ('','gametracker:rank update','/admin','193','0','$timestamp')");
  • And last cron file is for ranking players on servers: cron.players.php
<?php if(!isset($access_security) || $current_ip != $_SERVER['SERVER_ADDR']){die("direktan pristup zabranjen");}

$chars = array();
$start = date("H")+2;
for($i = 0; $i < 12; $i++){$chars[] = str_pad( ($start+2*$i)%24, 2, '0', STR_PAD_LEFT);}
$chart_updates = '|' . implode( '|', $chars);


$server_query   = mysql_query("SELECT id,ip,port,game,rank_pts,playercount FROM serverinfo ORDER BY rank_pts DESC");
while($server_row = mysql_fetch_assoc($server_query)){
    $server_id                  = $server_row['id'];
    $server_ip                  = $server_row['ip'];
    $server_port                = $server_row['port'];
    $server_game                = $server_row['game'];
    $server_rank_pts            = $server_row['rank_pts'];
    $server_playercount         = $server_row['playercount'];

    require_once("gameq.php");
    $servers = array('server_'.$server_id => array($server_game, $server_ip, $server_port));
    $gq = new GameQ();
    $gq->addServers($servers);
    $gq->setOption('timeout', 200);
    $gq->setFilter('normalise');
    $gq->setFilter('sortplayers');
    $data = $gq->requestData();


    if($data['server_'.$server_id]['gq_online'] == "1"){
        $num_players        = mysql_real_escape_string($data['server_'.$server_id]['gq_numplayers']);
        $max_players        = mysql_real_escape_string($data['server_'.$server_id]['gq_maxplayers']);
        $rank_pts           = $server_rank_pts + ($num_players/$max_players);
        $rank_pts           = str_replace(".", ",", $rank_pts);
        if($num_players < 10){$num_players = "0".$num_players;}
        $server_playercount = substr($server_playercount, 3);
        $server_playercount = $server_playercount.",".$num_players;


        $update_query       = mysql_query("UPDATE serverinfo SET num_players='$num_players', max_players='$max_players', playercount='$server_playercount', chart_updates='$chart_updates', last_chart_update='$timestamp', rank_pts='$rank_pts' WHERE id='$server_id'");
    } else {
        $server_playercount     = substr($server_playercount,3);
        $server_playercount     = $server_playercount.",00";
        $update_query           = mysql_query("UPDATE serverinfo SET online='0', playercount='$server_playercount', last_chart_update='$timestamp' WHERE id='$server_id'");
    }
}




$serv_count = mysql_num_rows(mysql_query("SELECT id FROM serverinfo"));
$user_count = mysql_num_rows(mysql_query("SELECT id FROM users"));
$plyr_count = mysql_num_rows(mysql_query("SELECT id FROM players"));
$tpcs_count = mysql_num_rows(mysql_query("SELECT id FROM topics"));

$stats_string .= '<p class="server_stats_block">'.$serv_count.'<br /><small>registrovanih servera</small></p>';
$stats_string .= '<p class="server_stats_block">'.$user_count.'<br /><small>registrovanih članova</small></p>';
$stats_string .= '<p class="server_stats_block">'.$plyr_count.'<br /><small>online igrača</small></p>';
$stats_string .= '<p class="server_stats_block">'.$tpcs_count.'<br /><small>tema na forumu</small></p>';

$statsfile = fopen("files/cache/homestats.cache", 'w'); 
fwrite($statsfile, $stats_string);
fclose($statsfile);

Here is cron links:
http://shareursocial.com/system/plain/cron.players.php
http://shareursocial.com/system/plain/cron.gametracker.php
http://shareursocial.com/system/plain/cron.serverrank.php

Thank you all,and sorry for my really bad english

I hope that some developer can help me to solve this.
Thank you again.

13 comments
  • It seems to me that it would be better to use curl for this, as php-cli doesn't act the same as Apache mod_php

    curl http://localhost/path_to_the_files/file.php > /dev/null 2>&1
    

    To verify this, I created a php file that just prints $_SERVER variables:

    echo "<?php
    > print_r($_SERVER);
    > ?>" > test_server_vars.php
    

    Here's what I got when running it from the CLI:

    PHP Warning:  print_r() expects at least 1 parameter, 0 given in /REDACTED/test_server_vars.php on line 2
    PHP Stack trace:
    PHP   1. {main}() /REDACTED/test_server_vars.php:0
    PHP   2. print_r() /REDACTED/test_server_vars.php:2
    
    
  • As @JonsJava said, the $_SERVER superglobal variable isn't available when running the script from the CLI.

    You can check if the script is running from the CLI like this:

    if(php_sapi_name() === 'cli') {
        // running from CLI
    }
    

    So you can replace

    <?php if(!isset($access_security) || $current_ip != $_SERVER['SERVER_ADDR']){die("direktan pristup zabranjen");}
    

    with

    <?php if(php_sapi_name() === 'cli' || !isset($access_security) || $current_ip != $_SERVER['SERVER_ADDR']){die("direktan pristup zabranjen");}
    
  • Fixing @kamaln7 comment:

    <?php if(!php_sapi_name() === 'cli' || !isset($access_security) || $current_ip != $_SERVER['SERVER_ADDR']){die("direktan pristup zabranjen");}
    

    It's checking for if it isn't these things, to deny access. Good catch though, @kamaln7 !

  • Oh, my bad! Thanks @JonsJava :)

  • Thank you for comment's @JonsJava and @kamaln7

    I'am also bad whit coding... :(

    But i delete this code:

    <?php if(!isset($access_security) || $current_ip != $_SERVER['SERVER_ADDR']){die("direktan pristup zabranjen");}
    

    and add this code instead:

    <?php if(!php_sapi_name() === 'cli' || !isset($access_security) || $current_ip != $_SERVER['SERVER_ADDR']){die("direktan pristup zabranjen");}
    

    On all my cron files.

    Sorry but i don't understud this about curl,can you please tell me what i need to do step by step ?
    Sorry for my bad english,and thanks a lot both of you,

  • BUMP..

    Please anyone whit new ideas ?

  • No worries. Sorry. I've been swamped all weekend, so couldn't get back to you sooner.

    Try this bit of code instead:

    <?php 
    if (php_sapi_name() != 'cli'){
        if(!isset($access_security) || $current_ip != $_SERVER['SERVER_ADDR']){die("direktan pristup zabranjen");}
    }
    //rest of code goes here
    
  • Thank you JonsJava,
    I added this code instead this:

    <?php if(!php_sapi_name() === 'cli' || !isset($access_security) || $current_ip != $_SERVER['SERVER_ADDR']){die("direktan pristup zabranjen");}
    

    But cron still don't work.

    I also install mailx on vps and all time i get email's like this:

    Not a directory: /var/zpanel/hostdata/zadmin/publichtml/shareursocialcom/system/plain/cron.serverrank.php

    Not a directory: /var/zpanel/hostdata/zadmin/publichtml/shareursocialcom/system/plain/cron.players.php

    Not a directory: /var/zpanel/hostdata/zadmin/publichtml/shareursocialcom/system/plain/cron.rent.php

    etc...

  • @kospicboris: It should actually look like this:

    <?php if(php_sapi_name() != 'cli' || !isset($access_security) || $current_ip != $_SERVER['SERVER_ADDR']){die("direktan pristup zabranjen");}
    

    There's also a missing '>' in your cronjobs:

    */1 * * * * /usr/bin/php /var/zpanel/hostdata/zadmin/public_html/shareursocial_com/system/plain/cron.rent.php >/dev/null 2>&1
    
  • @kamaln7 thanks for reply,

    Now i edit cron files again and change cron command,but servers still don't show on site.

    But email like this still come to me:
    Not a directory: /var/zpanel/hostdata/zadmin/publichtml/shareursocialcom/system/plain/cron.rent.php

    Mabey i need to try to use different cron command ?

  • Hello,

    When i try to run command manualy i get this error:

    PHP Warning:  mysql_query(): Access denied for user 'root'@'localhost' (using password: NO) in /var/zpanel/hostdata/zadmin/public_html/shareursocial_com/system/plain/cron.gametracker.php on line 3
    PHP Warning:  mysql_query(): A link to the server could not be established in /var/zpanel/hostdata/zadmin/public_html/shareursocial_com/system/plain/cron.gametracker.php on line 3
    PHP Warning:  mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /var/zpanel/hostdata/zadmin/public_html/shareursocial_com/system/plain/cron.gametracker.php on line 4
    
    

    Thanks you @JonsJava and @kamaln7 for helps.

  • They mysql errors mean that your scripts aren't getting the credentials from the configuration file. This means that you're not including that file, or this script wasn't designed to run directly.

  • @JonsJava
    Thanks for answer.
    But how i can run it if not directly ?

Be the first one to answer this question.