Need Some Cron Job Help

Discussion in 'PHP' started by zealot777, Dec 12, 2009.

  1. #1
    Hello,

    Can anybody give me some idea on how to do cron jobs? What is the file extension? Where will it be placed? Any basic sample code? Thanks in advance!
     
    zealot777, Dec 12, 2009 IP
  2. biggsk

    biggsk Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here is a guide on setting up cron jobs:
    http://www.adminschoice.com/docs/crontab.htm#Example

    I hope this helps.
     
    biggsk, Dec 12, 2009 IP
  3. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If you have cpanel you can set them in there using the cron job in the dashboard
    It's really easy to use
     
    JAY6390, Dec 12, 2009 IP
  4. zealot777

    zealot777 Peon

    Messages:
    448
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks but what i wanted to know is how to do it in a script as I need to add a cronjob to an autoblog script that i made. Anybody can teach me how to do that? Can I add cron jobs in a script? Will appreciate any help. Thanks!

    I already made one but it's not actually a cron job as site needs to be visited to be activated.
     
    zealot777, Dec 12, 2009 IP
  5. fierceservers

    fierceservers Peon

    Messages:
    338
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If the site needs to be visited then just issue a wget command, so for example:
    note - the hxxp is only cause I can't post links yet
     
    fierceservers, Dec 12, 2009 IP
  6. zealot777

    zealot777 Peon

    Messages:
    448
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yeah, that's currently what i made. But, I'm trying to find a way where it will be a cron job that will not rely to visits. Can anybody help? Thanks!
     
    zealot777, Dec 12, 2009 IP
  7. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Create a script that runs on your own cron job every minute, then with that script, run crons depending on if enough time has passed since the last time it was run
     
    JAY6390, Dec 12, 2009 IP
  8. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I'm guessing since you want this to happen via scripting that you want to do this quite flexibly, so consider using a database to read all the cron commands and the time periods between them
     
    JAY6390, Dec 12, 2009 IP
  9. zealot777

    zealot777 Peon

    Messages:
    448
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Yeah, that's exactly what i want to make. Can you teach me how to call a cron via a script? and how will i make the cron the i'll be calling via a script? Can you at least teach me some basics? Thanks!
     
    zealot777, Dec 12, 2009 IP
  10. Wogan

    Wogan Peon

    Messages:
    81
    Likes Received:
    3
    Best Answers:
    2
    Trophy Points:
    0
    #10
    Set up your PHP script:

    // Do Cron stuff here
    PHP:
    Save it somewhere accessible:

    /home/myhost/public_html/cronscripts/myscript.php
    Code (markup):
    Then set up a cronjob with the following parameters:

    * * * * * php /home/myhost/public_html/cronscripts/myscript.php
    Code (markup):
    Glossary:

    * * * * * Every second
    0 * * * * Every minute (at 0 seconds)
    0 0 * * * Every hour at (0 minutes, 0 seconds)
    ... and so on (refer to a CRON manual)
    Code (markup):
    To test if it works, have it send you an email every time it runs (and disable the code when you confirm that it works, otherwise you'll be swamped):

    // Do Cron stuff here
    
    // Mail to confirm
    mail('my@email.com', 'Cronjob Ran!', 'The cronjob worked');
    PHP:
     
    Wogan, Dec 13, 2009 IP
  11. zealot777

    zealot777 Peon

    Messages:
    448
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Great! thanks, will give it a try.
     
    zealot777, Dec 13, 2009 IP
  12. yoes_san

    yoes_san Peon

    Messages:
    443
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Never used server's cronjob before, I should try this out also, thanks!
     
    yoes_san, Dec 14, 2009 IP
  13. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #13
    if you're going to use php as a shell script, make sure trhe she-bang (or whatever it was called) is present in the php script header, just as you would in perl or bash:
    
    #!/usr/bin/php
    <?PHP
    
    .... 
    ?>
    PHP:
    also make sure you chmod it to 755 or something to allow execute
     
    dimitar christoff, Dec 15, 2009 IP
  14. thuankkk

    thuankkk Active Member

    Messages:
    503
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    95
    #14
    try some online cron jobs service such as www.setcronjob.com, onlinecronjob.com, webcron.org. You can set up cron jobs to call your URL periodically. It's very easy, as all of those have web cron interface.
     
    thuankkk, Dec 16, 2009 IP
  15. Wogan

    Wogan Peon

    Messages:
    81
    Likes Received:
    3
    Best Answers:
    2
    Trophy Points:
    0
    #15
    No, the hashbang (#!/usr...) only applies to shell scripts. You can't execute PHP scripts natively in the shell. What this does:

    php myscript.php
    Code (markup):
    ...is start PHP with "myscript.php" as a parameter. There don't have to be any special permissions on myscript.php in order to get it to run.

    All of this only applies to shell scripts, which need the hashbang, and need chmod +x in order to be executed like this:

    ./myscript.sh
    Code (markup):
     
    Wogan, Dec 16, 2009 IP
    thuankkk likes this.
  16. masterofweb

    masterofweb Greenhorn

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #16
    cron job is not a file... it is a service ,which is provided by the hosting server.if u r using in CPANEL then go to cronjob tab and give the URL of your file which u want run at a specific time.or if u use plesk then goes to crontab link and set timing and file path
     
    masterofweb, Dec 17, 2009 IP
  17. yoes_san

    yoes_san Peon

    Messages:
    443
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #17
    What about securities?

    It seems to me that anyone who somehow knows the location of the PHP script can access that page via browser and cause functions to fire.
    Are there anyway to prevent this so that PHP script can only be used with cron?
     
    yoes_san, Dec 17, 2009 IP
  18. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #18
    yeah, code it so it checks browser string, if any (i.e. not triggered by cron as a shell script), just do a 'die'.
     
    dimitar christoff, Dec 17, 2009 IP
  19. thuankkk

    thuankkk Active Member

    Messages:
    503
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    95
    #19
    Set a password, and it will be fine :)
    put this into your php file:
    
    <?php
      if($_GET['password']!='SECRET') exit('Access denied.');
    ?>
    PHP:
    Then just set cron job with yoursite.com/script.php?password=SECRET
     
    thuankkk, Dec 17, 2009 IP
  20. yoes_san

    yoes_san Peon

    Messages:
    443
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #20
    Ah I see, gonna try that out now - thanks.
     
    yoes_san, Dec 17, 2009 IP