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

Simple System Maintenance with PHP-CLI

By Akash Mehta
2008-01-21


Get Maintaining!

So, we've got the fundamentals of developing PHP-CLI scripts. Experiment with that a little further; especially try including your existing application's config and database files and running some SQL queries against your application's database. As this isn't a tutorial on PHP programming, we won't cover much specific code, as what you want to do with your code is up to you. With a bit of experimentation, however, you can easily unleash the full power of PHP-CLI.

So, what are we maintaining?

Chances are your existing web applications already have a number of ongoing maintenance tasks. If they don't, maybe they should? Here are some common tasks PHP-CLI is used for:

  • Removing old / expired entries from databases, say of cache files or message histories
  • Resetting statistics / clearing logs
  • Generating backup files

Many of these are handled manually by sysadmins, when they could be done just as well – maybe even more reliably – when automated with PHP-CLI. And some more interesting uses:

  • Sending reports to stakeholders on site popularity and usage statistics
  • Scanning for possible security breaches and locking down the site for "unexpected maintenance"
  • Connecting to remote machines via SSH and SCPing sensitive data (e.g. userdata backups) through

The possibilities are only limited by what you can do in PHP.

Tips for CLI scripts

The PHP manual mentions how the maximum execution time is unlimited for CLI scripts. This should probably be a good hint that anything time consuming – a backup of a 200MB database, for instance – is certainly possible in a PHP-CLI script. In fact, sometimes it's best done with a PHP-CLI script, as opposed to shell/batch scripting, which often results in functionality duplication – with a PHP-CLI script you can make use of existing PHP resources.

PHP-CLI scripts can be entirely automated, and do not necessarily have to call for user input, as we demonstrated in our example above – in fact, most of these scripts are designed to be called by other scripts and other applications, often serving as programmatic integration points between different areas of a technology stack. For example, a scheduled task of a PHP-CLI script could bridge the gap between a system's task scheduling service and a web application's data store.

This brings us to our final tip – scheduled tasks. On UNIX systems, crontab – a system for scheduling commands to be executed at scheduled intervals – is a common sight. With crontab, time-sensitive tasks can be achieved in web applications, which can prove highly valuable in certain situations. For example, you might want to process log data once a week, take a web application offline for a server restart at a certain hour of the day, or send out an auto-generated newsletter to users at the end of each month.

On Windows servers, crontab's general functional equivalent is Scheduled Tasks. PHP-CLI also makes this task scheduling somewhat cleaner: the traditional hack for PHP task scheduling on a Windows server was to open a web browser at the URL of a script, and to schedule a task one minute later to close the web browser. This can just as well be done via CLI. In fact, PHP on Windows systems comes with a "php-win.exe' binary, which is identical to PHP-CLI except that it omits the black DOS box, which can be somewhat distracting if the system is used as workstation as well as server.



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