Simple System Maintenance with PHP-CLI
By Akash Mehta2008-01-21
Just to give you a feel of what can be achieved with PHP-CLI, here are some examples of scripts in action. Typically these demonstrate how functionality can be achieved in the real world; you can take these ideas and develop similar scripts that suit your own application.
Database cleanup
This application is a chatroom. Instead of purging the database on every single page load – and, being a live chat room, there were many backend page loads – the database table was cleared regularly with a PHP-CLI script run with crontab. Here's the code:
#!/usr/bin/php
<?php
include("/var/www/includes/database.php');
// time() - 86400 = 24 hours ago.
$db->query("DELETE FROM `messages` WHERE `time` < ".time()-86400);
$db->close();
This script incorporates all the existing database code from the application – here, $db->query() is a method on an object created in the includes/database.php file of the main web application. By utilising this existing code, the script can achieve its intended functionality in just two lines of code.
Human-assisted maintenance
Now, there are many maintenance tasks that can be achieved automatically, but sometimes it makes sense to have a real person directing a maintenance job. This might be a one off, like an assisted database dump. Here's a sample of how I've achieved it in the past, making use of existing code in my application:
#!/usr/bin/php
<?php
include("/var/www/emc/inc.php');
// Authenticate user with application's auth system
fwrite(STDOUT, "Username: ");
$user = trim(fgets(STDIN));
fwrite(STDOUT, "Password: ");
$pass = trim(fgets(STDIN));
if (!EMC::auth($user,$pass)) die("Bad login.");
// Ask the user tables to exclude from the dump
fwrite(STDOUT, "Tables to exclude: ");
$tables = explode(",'trim(fgets(STDIN)));
// Ask them where to save the dump
fwrite(STDOUT, "Filename: ");
$file = trim(fgets(STDIN));
// EMCDB::dump() returns SQL queries to recreate the database
$sql = EMCDB::dump(0, $tables);
// If the user doesn't enter an absolute path, this is
// saved in the current path – useful for sysadmins.
file_put_contents($file,$sql);
fwrite(STDOUT, "Dump complete.\n");
A few things to notice here. First, instead of echo/print we're writing to STDOUT – this is the standard output stream, similar to STDIN, which we've already used for input; and STDERR, error output. Writing to STDOUT is just good practise, although there is some technical reasoning behind it. Next, we're authenticating the user. We can't just let anyone get an entire copy of our database so easily (although if an intruder has gained shell access, chances are they will anyway). Next, we're saving the database dump to the current directory. This highlights the value of context-sensitive PHP-CLI scripts: I could call the script from my home directory to make sure I've instantly got access to it; from a protected web-facing directory to give trusted users access to it etc. Finally, EMCDB::dump() is calling the mysql dump utility via command line itself; it has a call to shell_exec(). That is, PHP-CLI scripts can interact with other command line utilities, and that's probably the most power you'll get from your PHP-CLI scripts all day.
Tutorial pages:
|
|
|||||||||
You might also want to check these out:
|
Link to This Tutorial Page!

