Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 5 of 5
  1. #1
    Senior Member medlington's Avatar
    Join Date
    Nov 2005
    Location
    Sheffield, UK
    Posts
    377
    Member #
    11968
    Liked
    4 times

    What Can I and Cant I use in a Cron Job?

    Hi,

    Ive setup a Cron job that is working and is getting triggered by the server, at the moment it just sends me an email to show its working.

    As soon as I add the correct script that I want it to use it stops working but I cant figure out why. If I trigger the script through the browser it works fine but when triggered by Cron it doesnt work.

    Should I be able to use all PHP in a Cronjob or is it limited in anyway.

    Here is my script, it is supposed to find a random post out of my DB and post it to twitter:

    Code:
    $dbhost = "xxxxxxxxxxx";
    $dbuser = "xxxxxxxxxx";
    $dbpass = "xxxxxxxx";
    $dbname = "xxxxxxxxxxxx";
    
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
    mysql_select_db($dbname);
    
    $sql = "SELECT *
    FROM twitter
    ORDER BY RAND() LIMIT 1";
    $rs_result = mysql_query ($sql, $conn);
    while ($row = mysql_fetch_assoc($rs_result)) {
    $id = $row['id'];
    $message = $row['post'];
    }
    
    mysql_close($conn);
    
    // Load the app's OAuth tokens into memory
    require 'app_tokens.php';
    
    // Load the tmhOAuth library
    require 'tmhOAuth.php';
    
    // Create an OAuth connection to the Twitter API
    $connection = new tmhOAuth(array(
      'consumer_key'    => $consumer_key,
      'consumer_secret' => $consumer_secret,
      'user_token'      => $user_token,
      'user_secret'     => $user_secret
    ));
    
    
    
    $code = $connection->request('POST', 
        $connection->url('1.1/statuses/update'), 
        array('status' => "$message" ));
    
    // A response code of 200 is a success
    if ($code == 200) {
      print "Tweet sent";
    } else {
      print "Error: $code";
    }
    
    mail('xxx@xxx.com','Cron Job Test Script',"$message");
    Can anyone see any problems with it? Like I say if I trigger it directly from the browser it works fine.

    Thanks

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,693
    Member #
    5580
    Liked
    717 times
    Try commenting-out portions of the script to see where it might be stopping.

    Example, comment-out this line and see if you get an email:

    // $code = $connection->request('POST',$connection->url('1.1/statuses/update'),array('status' => "$message" ));

    And you know that the response code isn't doing anything when the script is executed by CRON. Unless CRON saves HTML output in a log of some kind. Or simply comment-out or remove the testing of the response code.

    Another thing to try .... in case it's a timing issue ...
    Put the mail() function within an 'if' statement to force PHP to finish it before the script ends.

    if(mail('xxx@xxx.com','Cron Job Test Script',"$message")){
    // function ended true
    }


  4. #3
    Senior Member Webzarus's Avatar
    Join Date
    May 2011
    Location
    South Carolina Coast
    Posts
    3,322
    Member #
    27709
    Liked
    770 times
    I normally just create a page that does what you want it to do... Test... Test... Test... When its completely working and debugged... I set the Cron job to just call the page... Then if I want to change the task... I make a copy ... Test... Test.. Test... Once its working I rename it back to the original... No mucking around with the control panel.. Cron job ...

    just me though

  5. #4
    Senior Member medlington's Avatar
    Join Date
    Nov 2005
    Location
    Sheffield, UK
    Posts
    377
    Member #
    11968
    Liked
    4 times
    Thanks,

    I have a feeling its something to do with the relative addressing with the require 'app_tokens.php';

    I did read that when the Cron is executing it does so from a different place than if you were executing it through a browser. I will have to do some more testing of it.

    @mlseim The response is emailed to me if the Cron executes the script

  6. #5
    Senior Member medlington's Avatar
    Join Date
    Nov 2005
    Location
    Sheffield, UK
    Posts
    377
    Member #
    11968
    Liked
    4 times
    Bit of an update, I tried different versions of this script and tested and tested and they all worked until I set them as a CRON. The I put it all on a different server and it worked like a charm.

    From reading up about it it seems that it is an issue with Twitter and session variables or something along those lines so that when activated from the browser it logs in and creates a session but not when Cron does it.

    Found people saying that the server needed to verifySSL but couldnt find a way to check this. Anyway all working now thanks for advice


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 11:09 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com