Home arrow PHP arrow PHP CLI and Cron
PHP

PHP CLI and Cron


In this article, Jason will explain what CLI and Cron are and how to use them together within the PHP environment.

Author Info:
By: Jason Lam
Rating: 5 stars5 stars5 stars5 stars5 stars / 73
April 05, 2003
TABLE OF CONTENTS:
  1. · PHP CLI and Cron
  2. · Cron
  3. · Final Words

print this article
SEARCH DEVARTICLES

PHP CLI and Cron
(Page 1 of 3 )

What is PHP CLI?

CLI stands for command line interpreter.  What does this do for you?  This allows you to run your PHP scripts at command line similar to Perl or any other scripting language used in *nix command line environment.

Why PHP CLI?

There a several advantages why one would want the ability to run PHP at command line

No need to learn another scripting language such as Perl, Bash or Awk

Ability to reuse existing components.  For example say you've written a set of database access objects or functions you now can re-use them instead of re-writing them in Perl, Java or some other language.

Cross-platform, you can easily run PHP on *nix environment or windows environment or in fact any OS with PHP CLI installed

Making GUI applications, this is out of scope for this article visit http://gtk.php.net to learn more about PHP and desktop applications.

One may ask why do I need to run PHP at command line in the first place? I'm a web developer.  Lots of times when making and running web applications with database functionality you will probably find yourself in position where you need to regularly clean up the database tables. 

Another example maybe you need to send out regular web reports generated by your application to the appropriate administrators.  Whatever the case may be, rather then trying to remember to run these tedious tasks on a regular basis isn't it better to have a batch process where these tasks are automatically done at specified times.  This is where you would use Cron and now that you know the advantages of using PHP at command line these two make a great pair. 

There really isnít much too it, it is just a matter of using the 2 different technologies together. The following sections will introduce you to both PHP CLI and Cron.  After it will be pretty clear how to use them together, but incase you still donít get it there is section demrating the use of PHP and Cron.

How to Install PHP CLI

Pre-Installed / Package Manager

If you are using Linux distribution with PHP already pre-installed or if you are using a package manager such as RPM you may have to do some fishing around to find the PHP executable.  Mostly likely its under /usr/bin, as friendly reminder you might want to run the find command to find the PHP executable.  Navigate you way to the root of your file system and type in the following find . -name php -print 2>&1 /dev/null. 

Install from Using tar/source

For those of you who like to install from source, it is almost the same as installing PHP with apache except without APXS.  To install PHP CLI you need to install it purely as CGI and with the --enable-cli option included.  For those of you who still want to use PHP for the web you will have to install a separate installation of PHP. 

Everything should work fine because with the APXS Apache when compiled with the right options will know where to find the PHP APXS version and for the other version of PHP all you have to do is set the correct PATH to access the PHP CLI version at command line.  Here is step-by-step install of PHP CLI

Download the latest stable release of PHP

$ tar- xvzf php-version.tar.gz
$ cd php-version/
$ ./configure --prefix=/usr/local/php
--enable-cli
--bindir=/usr/bin

Ö. any other options you normally put, for example --with-mysql if you are going to use mySQL database with PHP

$ make
$ make install

Note --bindir indicates where you want the PHP binary to be installed

Hello World

Create a test file called phpcli.php.  This file will contain a simply your standard hello world

#!/usr/bin/php -q
<?php
echo "Hello World with PHP CLI";
?>

Change the permission on the file to be executable, chmod 755 phpcli.php or chmod +x phpcli.php

Now run it, assuming you are in the same directory and the Path is indeed set for /usr/bin.

$ ./phpcli.php
Hello World with PHP CLI
$

You will notice the difference from web php script there is the line #!/usr/bin/php, this indicates that the PHP executable is used to parse the file.
Reading in Command Line Arguments
Like most scripting languages you are able to pass in command line arguments.  Create a file named testargs.php

#!/usr/bin/php -q
<?php
 echo "Test Read Arguements:\n\n";   
 echo $_SERVER["argc"];     
 echo "\n";   
 echo $_SERVER["argv"][0];
?>

$_SERVER["argc"] will give the number arguments enter including.  Note the script itself is an argument.

$_SERVER["argv"] is an array of arguments.  To access the first argument it will be at index 1, $_SERVER["argv"][1].  Remember the file name itself is at index 0, $_SERVER[argv"[0].

Notice as well now that we aren't making a web script we need to apply CLI specific code in this case to indicate next line we type in the escape character followed by n.  Instead of <br> which is normally used for html.

More Hello World

Another way to run php at command line is directly right at the command line, for example

$ php -r "echo 'Hello World'";
Hello World
$


Getting more Help with PHP CLI

Type in the php executable with the switch -h, php -h.  It should output something similar to the following:

Usage: php [options] [-f] <file> [args...]      
          php [options] -r <code> [args...]      
          php [options] [-- args...]

-s        Display colour syntax highlighted source. 
-w       Display source with stripped comments and whitespace. 
-f        <file> Parse <file>. 
-v        Version number 
-c        <path>|<file> Look for php.ini file in this directory 
-a        Run interactively 
-d        foo[=bar] Define INI entry foo with value 'bar' 
-e        Generate extended information for debugger/profiler 
-z        <file> Load Zend extension <file>. 
-l         Syntax check only (lint) 
-m       Show compiled in modules 
-i         PHP information 
-r         <code> Run PHP <code> without using script tags <?..?> 
-h        This help 
args...   Arguments passed to script. Use -- args when first argument starts with -            or script is read from stdin

Next: Cron >>

blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- 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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials