+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: CRON Jobs and PHP

  1. #1
    deadimp is offline x10 Sophmore deadimp is an unknown quantity at this point
    Join Date
    Jun 2007
    Posts
    249

    CRON Jobs and PHP

    CRON jobs according to cPanel:
    Cron jobs allow you to automate certain commands or scripts on your site. You can set a command or script to run at a specific time every day, week, etc. For example, you could set a cron job to delete temporary files every week so that your disk space is not being used up by those files.
    This tutorial is here to provide basic information on executing php scripts for your CRON jobs.
    There are two parts to your crontab: the command to run, and the interval you run it on. I won't go over how to set the intervals because cPanel has a nice interface that makes it simple for beginners or advanced users.

    The way to have CRON execute your php script is to call the php interpereter and tell it to process that file. Keep in mind that it's a command and not a file (unless it's a shell/executable) you're entering. Example:
    Code:
    php -q /home/username/bin/script.php
    For your php script, you enter the path on the server (according to the *nix filesystem), not as a URL.

    You usually don't want to expose your CRON job script in your html/web directory (/public_html, /www), since the script most likely does maintainence that could be cumbersome and might be the base of a DOS attack or an even larger security risk.
    I just put mine in the /etc directory. That way no one can access/execute it via normal HTTP access. [/bin may be a better choice. I'm not sure if really matters past organization.]

    Also remember that in this way PHP is not running as a CGI module, meaning you won't have access to the environment variables you're used to, like $_GET, $_POST, etc.
    If you want to refine your control, keep in mind that you have access to the arguments passed to the script via $_SERVER['argv'] (which are like $_GET). You can pass commands like this: "php -q /blah/script.php -clear".
    If you used print_r($_SERVER['argv']), it would return a printout for array('/blah/script.php','-clear').

    NOTES:
    * I don't know what the -q flag does, though... Corey had suggested it here.
    Can't find it via `php --help` on my XAMPP installation.
    * You might be able to run the script just using the script's name without the php command - assuming the system will recognize its type via filename extension - but I'm not sure. I can't test it at the moment because CRON is disabled for me right now (1/30/0.
    deadimp.org
    > Thacmus [v0.3.0] - Open source CMS, PHP 5, MySQL

    "¡Ya lo sabo!" - Lo Ausente

  2. #2
    Fahad Guest

    Re: CRON Jobs and PHP

    Nice tut...
    Curious - what would happen if you used an echo()?
    Btw, it wouldn't recognize by extension, as this is linux, not windows.

  3. #3
    deadimp is offline x10 Sophmore deadimp is an unknown quantity at this point
    Join Date
    Jun 2007
    Posts
    249

    Re: CRON Jobs and PHP

    Curious - what would happen if you used an echo()?
    Gah! Forgot to mention that!

    Any output from your script (i.e. echo) is sent to the email you've specified in your CRON options.
    If you have CRON run a command `php -q /home/user/bin/test.php` with this as the file:
    Code:
    <?php
    echo "This is a test";
    ?>
    You'll receive a message like this on each execution:
    Code:
    Subject: Cron <user@server> php -q /home/user/bin/test.php
    Sender: Cron Daemon <root@server>
    This is a test
    I would suggest creating a filter on your email client to lump all of your CRON messages into one folder, deleting them every so often.

    Btw, it wouldn't recognize by extension, as this is linux, not windows.
    Thanks for the info!
    I'm wondering, though, if you could use specify execution options using the 'shell line' (like what you would do with a bash script) like so:
    Command:
    Code:
    /home/user/bin/test.php
    File:
    Code:
    #!/bin/php
    <?php
    //Not sure where php is...
    echo "This is a test";
    ?>
    Don't know how that would work out in php with the tags and such... Maybe someone else could shed some light on that.
    Last edited by deadimp; 02-02-2008 at 12:01 AM.
    deadimp.org
    > Thacmus [v0.3.0] - Open source CMS, PHP 5, MySQL

    "¡Ya lo sabo!" - Lo Ausente

  4. #4
    Fahad Guest

    Re: CRON Jobs and PHP

    That would work, yes, but it would mean you HAVE to execute it without the php -q

  5. #5
    deadimp is offline x10 Sophmore deadimp is an unknown quantity at this point
    Join Date
    Jun 2007
    Posts
    249

    Re: CRON Jobs and PHP

    Well, you could still execute it with the shell lines as a direct command to php, but the line would just be included on the output as some 'extra noise' if you don't clear the output buffer (something like ob_clean() on the first line of the php code). Of course, that would be too much work to be worth not manually typing out the command.
    deadimp.org
    > Thacmus [v0.3.0] - Open source CMS, PHP 5, MySQL

    "¡Ya lo sabo!" - Lo Ausente

  6. #6
    oiwio is offline x10 Sophmore oiwio is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    214

    Re: CRON Jobs and PHP

    Nice tutorial. Ive been looking for one for a while

    I have one question though, if you want to connect to the database, do you have to use a module, or a normal database query?

    http://alchemistrpg.com: This site is worth

  7. #7
    Fahad Guest

    Re: CRON Jobs and PHP

    mysql_*()

  8. #8
    deadimp is offline x10 Sophmore deadimp is an unknown quantity at this point
    Join Date
    Jun 2007
    Posts
    249

    Re: CRON Jobs and PHP

    You can use just about any library/module you normally have access to under your CGI environment (ex. viewing your web pages).
    As fahadsadah pointed out, you can use the mysql module.
    deadimp.org
    > Thacmus [v0.3.0] - Open source CMS, PHP 5, MySQL

    "¡Ya lo sabo!" - Lo Ausente

  9. #9
    Dan's Avatar
    Dan
    Dan is offline
    Lord Of The Keys Dan is an unknown quantity at this point
    Join Date
    Aug 2007
    Location
    UK
    Posts
    1,258

    Re: CRON Jobs and PHP

    Thanks for this tutorial!! Very Useful
    Added to your Rep
    Thanks,
    Dan
    _______________
    Retired Staff

  10. #10
    FalseHope's Avatar
    FalseHope is offline Lord Of The Keys FalseHope is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    IRC
    Posts
    1,639

    Re: CRON Jobs and PHP

    Damn. nice tutorial! I never did know what they were for, well I knew they were some sort of automatic system. Thanks for the tutorial :D

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. cron jobs help needed
    By Killa619 in forum Scripts & 3rd Party Apps
    Replies: 1
    Last Post: 04-08-2008, 03:06 PM
  2. cron jobs help
    By Killa619 in forum Free Hosting
    Replies: 4
    Last Post: 11-13-2007, 05:02 AM
  3. Cron Jobs
    By stuffradio in forum Scripts & 3rd Party Apps
    Replies: 9
    Last Post: 11-12-2007, 07:01 PM
  4. Cron daemon problem
    By carclub in forum Free Hosting
    Replies: 2
    Last Post: 10-29-2007, 02:09 AM
  5. cron jobs help
    By TarinC in forum Scripts & 3rd Party Apps
    Replies: 5
    Last Post: 09-19-2006, 03:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers