Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

Simple System Maintenance with PHP-CLI

By Akash Mehta
2008-01-21


Fundamentals

Let's start with some basic details of how PHP-CLI works. If you've experimented with the CLI before, you can probably skip over this section.

What is PHP-CLI?

Command Line Interfaces, or CLIs, essentially provide access to the system via a simple text-based command interpreter. As the commands are nothing but text, they can be handled programmatically by other applications. This makes them highly interoperable and flexible – for example, PHP could be executed by another application to provide some functionality only available in PHP; in fact, we'll cover this point in detail in a moment.

Since PHP 4.3.0, PHP has by default come with a SAPI, a Server Application Programming Interface, named PHP-CLI. This interface was designed for running PHP scripts at the command line, opening new possibilities for uses of PHP.

PHP scripts for the CLI not only have all the power of your existing PHP scripts, they are also designed to interface with the system much closer than typical web pages. As a result, they're well suited for managing systems, as well as bridging the gap between the sysadmins and developers. Developing for the command line is somewhat different to traditional web page development:

  • User input can be handled at runtime, instead of requiring the user to set all options for the task to be executed before running the application/script.
  • The user sees the output as it is generated
  • Execution times can be well above the typical 1-2 seconds of a web page
  • Output is as plain and unformatted as possible; in fact, output is sometimes designed to be deliberately predictable so that it can be used by other programs
  • The physical location on the file system that the user is currently working with has added importance

We'll cover the impact of these in a moment.

But why PHP?

A question commonly asked of PHP-CLI is why it exists in the first place. Perl, for instance, has long been used in similar situations, and is arguably a more effective choice for building command line utilities. PHP was built with web pages in mind and its feature-set (and, therefore, part of its overhead) reflects this. There are some very compelling reasons to use PHP in CLI mode, however:

  • You already know the language, and you're a web developer, not a Perl/Bash/Awk scripter
  • It integrates well with existing PHP web applications
  • You can automate your web application's time-consuming maintenance tasks
  • You can reuse existing code, of which PHP has arguably one of the largest repositories

I'm not advocating entirely replacing Bash scripts with PHP-CLI, and if you're serious about building command line utilities you should certainly explore Perl, but for the typical web developer being able to use the language you know and love (or not) is the best bonus.

Differences with normal PHP

PHP-CLI scripts function almost identically to the standard PHP scripts you're used to. In fact, if you're using PHP under windows and your PHP executable is in your system's PATH, you can call your existing PHP scripts at command line already, with the simple command php myfile.php. There are a few basic differences in the scripting environment that you should be aware of; all are documented in the PHP manual and I encourage you to briefly read through this manual page before proceeding. There are a few important things to note:

First, the STDIN and STDOUT streams handle input and output (although basic echo/print will still work for output). A simple call to fgets(STDIN) allows you to take input from the user, which may be useful in accomplishing various tasks such as authentication and task direction. As a matter of good practise, you should use fwrite() on the STDOUT and STDERR streams for your general output and errors respectively.

Second, the working directory of the script – available from getcwd() – reflects the location you called the script from, not the location the script's main file itself resides in. That is, PHP does not change the current working directory for the purposes of your script, and this creates immense potential for context-sensitive scripting.

Let's examine this before moving on. What if you had a number of directories you wanted to index for your new photo album, but needed to hunt around your filesystem for them first? No matter – just put a PHP-CLI script in your path, roam around your console, cd'ing to different directories, and call add_to_index when you come across another folder, where add_to_index is a very simple PHP script to add the current folder (from getcwd()) to a MySQL database. Your existing web applications can then tap into this data store and publish your new photos regularly. Sure, you could just as well achieve this in Perl or Python – maybe even a shell script – but what if you're already familiar with PHP? PHP-CLI takes all your existing PHP skills and gives you a whole new world to explore.

Finally, the php.ini directive register_argc_argv is set to TRUE – as a result, the PHP variables $argc and $argv are set with appropriate values of command line arguments to your PHP script. That's right: your PHP scripts can take command line arguments. And other applications that can run commands at the shell can call your PHP scripts, supply data to them and interact with all your existing PHP code. It's often been said that, on a brand new GNU/Linux system, the basic utilities that come with the distribution are the most powerful. Now your PHP scripts can be equally powerful, without needing to learn a whole new language.



Tutorial Pages:
» Simple System Maintenance with PHP-CLI
» Fundamentals
» PHP-CLI 101
» Get Maintaining!
» Sample Scripts
» Have Fun!


Related Tutorials:
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1
» Desktop Application Development with PHP-GTK

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources