As many of you would surely attest, once you start developing with PHP it's pretty difficult to consider another language for your daily Web development activities. This isn't surprising, given PHP's relatively shallow learning curve, the literally thousands of readily available predefined functions, and the seemingly endliness array of projects and user groups sprouting up all over the globe. Wouldn't it be great if you could extend your use of the language to carry out general system administration tasks? You might be surprised to learn that you can! In this article, I'll show you how to create PHP scripts that can execute from the command-line. Once I've introduced a few basic concepts, I'll demonstrate how to write PHP scripts that can carry out useful administration tasks.
The Web server is responsible for processing requests and returning output to the client. Yet, the server isn't always capable of processing the request on its own, and depends upon a host of other tightly integrated technologies such as PHP and Perl for carrying out operations such as assembling page components, querying databases, and managing user sessions. For example, the ApacheWeb server knows when it must pass along a requested page to a third-party technology by examining the resource extension. For example, if the requested resource is the file index.php, and Apache has been configured to pass all resources with the extension .php to the PHP engine, then Apache will do so and wait for the result to be returned before responding to the client.
Although many have attempted to use PHP as the "golden hammer" in the past, this strategy often proves highly inconvenient simply because server-based tasks were simply never meant to run over extended periods of time. However, as of PHP version 4.2.0, it's possible to directly invoke the PHP engine by calling it from the operating system command-line, available via a new SAPI type known as the CLI (Command Line Interface). This new capability opens up the possibility for system administrators to use PHP for managing various server-related tasks, among which include duties pertinent to managing system backups, monitoring system resources, and parsing logs. This capability was disabled by default until version 4.3.0; therefore, if you're using a version 4.2.X version, you'll need to explicitly enable it by using the --enable-cli option at configuration time.
To determine whether your installation is capable of executing via the command-line, locate the PHP binary and execute it like so:
For example, my PHP 5 installation returns:
PHP 5.0.0RC1 (cli) (built: Mar 18 2004 18:20:03) Copyright (c) 1997-2004 The PHP Group Zend Engine v2.0.0RC1, Copyright (c) 1998-2004 Zend Technologies
You can view a complete list of options made available via this new CLI by executing the following command. I suggest doing so, as there are quite a few unexpected options that you may find quite useful.
In the next section, I'll demonstrate a few real-world administration scripts that you might consider incorporating into your own toolset.
A Simple Example
For starters, let's consider a simple yet useful example. I regularly work with colleagues based out of offices located in both GMT and PST time zones, and we're always calculating zone variations when suggesting meeting times for online chat conferences. Because I'm lazy, I wrote the following script that calculates the time differences, titled timezone.php:
<?php $christz = 0; // GMT $jasontz = -5; // GMT -5 $julietz = -8; // GMT - 8 echo "Jason: ".gmdate("h:i:sa", time() + 3600*($jasontz+date("I")))."\n"; echo "Chris: ".gmdate("h:i:sa", time() + 3600*($christz+date("I")))."\n"; echo "Julie: ".gmdate("h:i:sa", time() + 3600*($julietz+date("I")))."\n"; ?>
The script timezone.php can be executed from the command-line like so:
%>php timezone.phpSample output follows:
Jason: 10:41:27pm Chris: 03:41:27am Julie: 07:41:27pm
If you've created Perl, Python, or shell scripts in the past, this process should be quite familiar to you. However, you may have noted a few differences, several of which I'll outline here:
- Is the .php extension required? Nope. Because I use a blend of PHP, Perl, and shell scripts for administration, I prefer to retain the extension simply for sake of organization.
- Where's the shebang? Readers familiar with typical Perl and shell scripting are familiar with the #! reference on the first line, also known as the "shebang." The shebang is followed by the path leading to the scripting executable, /usr/bin/perl for example. In fact, you can do this with PHP, and forego the explicit reference to the PHP executable on the command-line; I prefer to do so in order to eliminate platform-compatibility issues.