Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts

    Cron Job Problem

    hi guys, im trying to get this cron job to run every hour on the hour, however it is proving difficult for me, as ive never used them before.

    in a short way of saying it, the script is ment to be triggered every hour on the hour, however i think ive gone wrong somewhere, the file is placed above the webroot, just like my host told me to.

    below is a picture of my control panel and the 2 current cron jobs ive tried, so if i have done it wrong, can you please advise me.

    also here is my script, so if anything is wrong, which i think some of the includes are, except for the connect.php one as thats in the same folder (above web root) however the functions one is in the includes folder which is in web root.

    PHP Code:
    <?php
    include('connect.php');
    include_once(
    'includes/inc-functions.php');
    $bulletquerys mysql_query("SELECT * FROM bf WHERE producing !='No'");

    // Loop through all rows in returned by the query
    // This will iterate for every row returned by your query.
    while($bulletquery mysql_fetch_array($bulletquerys)){
        
    $utime=$bulletquery['nextdue'];
        
    $now=time();
        
    $newbullets$bulletquery['stock'] + $bulletquery['add'];
        
    $nextdue$now+(60*60);


        if (
    $bulletquery['newprice'] != $bulletquery['price']) {
        
    $newprice $bulletquery['newprice'];
        }
        elseif (
    $bulletquery['newprice'] == $bulletquery['price']) {
        
    $newprice $bulletquery['price'];
        }
       
            
    mysql_query("UPDATE bf SET nextdue='".dbSafe($nextdue)."', stock='".dbSafe($newbullets)."', price='".dbSafe($newprice)."' WHERE id='".dbSafe($bulletquery['id'])."'");
       
     
    $to "dos1392@hotmail.co.uk";
     
    $subject "Cron Job";
     
    $body "it worked";
     if (
    mail($to$subject$body)) {
       echo(
    "<p>Message successfully sent!</p>");
      } else {
       echo(
    "<p>Message delivery failed...</p>");
      }

       
        
    }

    ?>



    Cheers in advance

    Dan
    Last edited by Dan13071992; 02-29-2012 at 06:33 PM.
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #2
    New Coder
    Join Date
    Sep 2010
    Posts
    65
    Thanks
    4
    Thanked 0 Times in 0 Posts
    try
    Code:
    crontab -l
    \
    to list all your cron jobs.

    however your cronjobs looks good for me, you can try simple php code to test if its working or not

    PHP Code:
    <?php
    $fp 
    fopen('test.txt''w');
    fwrite($fp'its working.....');
    fclose($fp);
    ?>
    and usually the address is /home/admin/..... if you are using vps
    Last edited by TheCracker; 02-27-2012 at 07:42 PM.

  • #3
    New to the CF scene
    Join Date
    Feb 2012
    Location
    Newbury Uk
    Posts
    7
    Thanks
    0
    Thanked 2 Times in 2 Posts
    To set the cronjob to be executed every hour you really need to state it as
    0 * * * * php dosomejob.php

    what You might add is to state default user who is going to run this job and add quotes to path (just to be sure and it looks nice ). Something like 0 * * * * cronuser php "dosomejob.php"

    One thing that comes to my mind is if cronuser that is currently running Your tasks has right to run them. Mostly the one in your home directory.

    Oooh and if you have access to logs you might find some useful info there!

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,310
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    I think you might need the path to PHP at the top of the file.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    i changed the includes to this:

    PHP Code:
    include('/home/crimewav/connect.php');
    include_once(
    '/home/crimewav/public_html/includes/inc-functions.php'); 
    however it is still not working, also im on shared hosting, not a VPS
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,310
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Dan, I think you need the path to php in the top of the script.

    When you call a php script via the browser, the webserver is configured to pass it to php which does its thing and passes the generated content back to the webserver which then sends it to the users browser.

    As a cron job there is no web server involved. That means that the operating system needs to know what program to open the file with. Unlike windows, linux systems don't always go by file extensions and often you'll need to put a path to the program on the top line - just like with perl scripts.

    I may be wrong - some servers don't need it however I've had to do this on a couple of scripts running under cron.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #7
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    so if i was to set the file type at the top of the page, would i do it like so:

    [code]
    /bin/php
    [/php]
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,310
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Yes only I can't remember if you might need the hash # in front. Also obviously you'd need to check the path to php on your server.

    A google of cpanel php cron might answer the exact details re the path and hash but I think that might be the issue (I could be wrong though so no promises - it's been a while since I've used cron!).

    Edit:
    I looked at an email pipe script I used to use (no not a very reliable one before you ask hence I abandoned it) and at the top that has this:

    #!/usr/bin/php -q
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #9
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    doesnt the # comment it out?

    also, would it go inside or outside of hte php tags "<?php" ?
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #10
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,310
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    In some languages, yes the # preceeds a comment however the operating system itself isn't a language - it's a system.

    The line goes on the first line. It's not part of the php language it's just telling the system where the interpreter can be found so it does not go inside the <?php tags - only php code goes inside there.

    As an example:
    #!/usr/bin/php -q
    <?php
    // read from stdin
    $fd = fopen("php://stdin", "r");

    Again, I can't guarantee this will solve your problem but I think its a step in the right direction.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #11
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    ok so ive updated my coding and there is a picture below of what i have tried on cron.



    Edit:
    btw ive changed the time from 0 to another number inorder to test instead of waiting for the hour.


    PHP Code:
    #!/usr/bin/php -q
    <?php
    include('/home/crimewav/connect.php');
    include_once(
    '/home/crimewav/public_html/includes/inc-functions.php');
    $bulletquerys mysql_query("SELECT * FROM bf WHERE producing !='No'");

    // Loop through all rows in returned by the query
    // This will iterate for every row returned by your query.
    while($bulletquery mysql_fetch_array($bulletquerys)){
        
    $utime=$bulletquery['nextdue'];
        
    $now=time();
        
    $newbullets$bulletquery['stock'] + $bulletquery['add'];
        
    $nextdue$now+(60*60);


        if (
    $bulletquery['newprice'] != $bulletquery['price']) {
        
    $newprice $bulletquery['newprice'];
        }
        elseif (
    $bulletquery['newprice'] == $bulletquery['price']) {
        
    $newprice $bulletquery['price'];
        }
       
            
    mysql_query("UPDATE bf SET nextdue='".dbSafe($nextdue)."', stock='".dbSafe($newbullets)."', price='".dbSafe($newprice)."' WHERE id='".dbSafe($bulletquery['id'])."'");
       
     
    $to "dos1392@hotmail.co.uk";
     
    $subject "Cron Job";
     
    $body "it worked";
     if (
    mail($to$subject$body)) {
       echo(
    "<p>Message successfully sent!</p>");
      } else {
       echo(
    "<p>Message delivery failed...</p>");
      }

       
        
    }

    ?>
    however its still not working :s am i missing anything else?
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #12
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,310
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    If you have the path to php in the file I think you can remove the /bin/php from the actual cron command.

    If that makes no difference, then undo my recommendations as they've clearly not worked.

    Edit:
    Note that cron jobs in php are not easy to debug. If your require_once lines cause an error the script will probably abort and you'll see no errors.

    To test that A script is executed successfully, run a simple script such as a one line call to mail() that will email you a confirmation that it ran.
    Last edited by tangoforce; 02-27-2012 at 09:32 PM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #13
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    done that, got the email

    PHP Code:
    42    *    *    *    *    /bin/php /home/crimewav/public_html/email.php 
    that was the cron settings

    Edit:

    it seems that using:

    /bin/php /home/crimewav/email.php

    didnt work even when i put that same file above the web root. any ideas why?

    Last edited by Dan13071992; 02-27-2012 at 09:46 PM.
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #14
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,310
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Your server probably restricts what can run above public_html. That said it may also restrict PHPs access outside of the public_html too which would explain your inclusion problems.

    Not all servers allow you to put files outside of public_html / www so if you can't then you probably want to hide your connect.php somewhere in some random folders OR encode it and decode it with a hardcoded password in your script. Food for thought.

    Anyway, good to hear you got the cron working - did you need the first line at the top of the file in the end?
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #15
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    my host actually told me to put it above webroot :S lol connect.php works fine there, i figured out my problem from the last post by spelling something wrong, im still having issues with the cron job but i have emailed the host to see what they had to say about it.
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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