Thread: Cron Job

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    56
    Rep Power
    4

    Cron Job


    I have this script:

    Code:
    <?php
    if($_GET['key']=="a"){
    #!/usr/local/bin/php -q
    
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    
    $sql="SELECT * FROM $tbl_name3 WHERE review_show='n'";
    $result=mysql_query($sql);
    $num_results=mysql_num_rows($result);
    if($num_results > 0){
    if($num_results==1){
    $message="You have ".$num_results." review unapproved.";
    }
    else{
    $message="You have ".$num_results." reviews unapproved.";
    }
    mail('webmaster@ghosthuntersportal.com','GHP Reviews', $message, 'From: sales@ghosthuntersportal.com');
    }
    
    $sql2="SELECT * FROM $tbl_name4 WHERE rma_issued='n'";
    $result2=mysql_query($sql2);
    $num_results2=mysql_num_rows($result2);
    if($num_results2 > 0){
    if($num_results2==1){
    $message="You have ".$num_results2." RMA Number Requested.";
    }
    else{
    $message="You have ".$num_results2." RMA Numbers Requested.";
    }
    mail('webmaster@ghosthuntersportal.com','GHP RMA Number Requests', $message, 'From: sales@ghosthuntersportal.com');
    }
    echo "Emails Sent.";
    }
    ?>
    with a Cron Job Command of /usr/bin/php -f /home/zyquo/public_html/ghosthuntersportal.com/cj_run.php?key=a

    It's set to execute once every minute... and I get nothing at all.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,484
    Rep Power
    1752
    I've never run php as a 'script', either directly or via cron but ... I do know the shebang line (the #!/usr/bin/php) HAS to be the first line of the script if you are wishing the shell to interpret it as php. What does yor line in cron that invokes this look like?
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,453
    Rep Power
    4539
    Also I don't believe you can pass '..?a=whatever' get arguments or use $_GET from a cli php script. GET is populated from data provided by a web server, and you're not using a web server from cli scripts.
    ======
    Doug G
    ======
    Bartender to Rene Descartes "have another beer?" Descartes: "I think not" and he vanished.
    --Alfred Bester
  6. #4
  7. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    Originally Posted by SimonJM
    I've never run php as a 'script', either directly or via cron but ... I do know the shebang line (the #!/usr/bin/php) HAS to be the first line of the script if you are wishing the shell to interpret it as php.
    Right. Since the cron command invokes /usr/bin/php the # will be interpreted as a comment and ignored.

    Originally Posted by Doug G
    Also I don't believe you can pass '..?a=whatever' get arguments or use $_GET from a cli php script. GET is populated from data provided by a web server, and you're not using a web server from cli scripts.
    Also right. I don't know how cron runs commands but that ? might even be interpreted as a metacharacter (by cron, by the shell, by somebody). If not then the system is looking for a file named "cj_run.php?key=a" (which doesn't exist).
    PHP would have dumped a message to stderr (which isn't captured in cron logs IIRC) and quit, thus no output.

    On that note, CLI scripts can use $argv:
    PHP Code:
    if (!empty($_GET["key"])) $key $_GET["key"];
    else if (!empty(
    $argv[1])) $key $argv[1]; 
    Code:
    /usr/bin/php -f /path/cj_run.php a
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    56
    Rep Power
    4
    Got this working... removed the key=a and placed it outside the public_html folder so it be accessed by a web browser.

IMN logo majestic logo threadwatch logo seochat tools logo