PHP
  Home arrow PHP arrow Page 2 - PHP CLI and Cron
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

PHP CLI and Cron
By: Jason Lam
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 54
    2003-04-05

    Table of Contents:
  • PHP CLI and Cron
  • Cron
  • Final Words

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    PHP CLI and Cron - Cron


    (Page 2 of 3 )

    What is Cron?

    Cron is basically a daemon used to initiate timed events.  As mentioned before a great use of this is to cleanup a set a database tables at a specified time.  It is usually automatically installed by default with most Linux distributions. 

    How to Use Cron?

    To access Cron simply type crontab -e, this usually invokes VI the default editor used to open crontab text file.  To change the default editor simply edit the /etc/.bash_profile then find the line that says editor=vi and change it to your favorite editor; for example, editor=emacs, editor=jove or editor=pico.  Along with crontab -e, creation of crontab entry there is crontab -l which will display the contents of the crontab file and crontab –r will clear the contents of the current crontab file.

    There may or may not be already entries in the file.  To add a new Cron entry you first need to understand what the file layout is.  The first set of numbers indicates the time to initiate or trigger the job the second part after the numbers is the process or task you want to run at the specified time.

    The first six fields (the time) are defined as follows

    FieldFONT>Definition
    Minute00 to 59
    Hour00 to 23 (military time)
    Day1 to 31
    Month1 to 12
    Weekday0 to 6 (where 0 is Sunday)

    An example of what a crontab entry looks like

    0 2 1 * * echo "Hello World" 2>&1 /dev/console

    This will output Hello World to the console at 2 am the first day of every month.  The * is a wildcard, meaning to match all values.  You can also specify a set and range of times.  Taking the same example, say you want to execute now at 2am and 2pm.  The entry would look like this

    0 2,14 1 * * echo "Hello World" 2>&1 /dev/console

    Notice the comma indicates more then one time for that field.  Say now wanted Hello World to output every hour from 2am to 2pm the entry could look like this

    0 2-14 1 * * echo "Hello World" 2>&1 /dev/console 

    PHP and Cron

    This is actually quite easy now that you know how to use both PHP CLI and Cron.  It is just a matter of using them together.  Referring to the Hello World php script phpcli.php, assuming resides it /home/phpscripts. Simply add an entry into Cron invoking this process everyday at 1 pm, the entry should like the following:

    0 13 * * * /usr/bin/php -f /home/phpscripts/phpcli.php

    More PHP Articles
    More By Jason Lam


       · This is obviously an old article, but even so there is no excuse for recommending...
     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT