August 21, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

System Administration with PHP

  • August 30, 2004
  • By W. Jason Gilmore
  • Send Email »
  • More Articles »

Redirecting Output

Although its useful to view script output from the terminal, you may wish to redirect this data to a textfile or even send it to somebody via e-mail. Both output variants are easily accomplished. In this section, I'll show you how both tasks are easily accomplished.

Redirecting Output to a Text File

Because we're dealing with shell-based scripting, we can take advantage of the full range of Unix redirection capabilities. For example, suppose that you'd like to redirect output from the timezone.php script to a file named output.txt:

%>php timezone.php > output.txt

Executing this command, you'll see that a file named output.txt has been created, with output contents identical to those found in the previous example. In the next installment of this series, I'll introduce a somewhat more sophisticated mechanism for redirecting script input and output, so be sure to stay tuned for more information on this topic.

Redirecting Output to an E-Mail

Just like my morning cup of coffee, I cherish the ability to have various statistics and other server information e-mailed to me for review at the beginning of each workday. I've long done so with my Perl and shell scripts, and now do the same with my PHP scripts, a feat easily accomplished using PHP's mail() function. Let's revise the original timezone.php script, calling it timezone-email.php, this time sending the output to my e-mail address (my address a tad obfuscated to fool annoying spammers):

$jasontz = -5; // GMT -5
$christz = 0; // GMT
$julietz = -8; // GMT - 8

$output = "Jason: ".gmdate("h:i:sa", time() +
$output .= "Chris: ".gmdate("h:i:sa", time() +
$output .= "Julie: ".gmdate("h:i:sa", time() +

mail("","My Colleagues' Local Time", $output);

Change the address, and execute this script in the same fashion as in the first example. After a few moments, the e-mail should arrive in your inbox.

Scheduling Script Execution

While manual script execution is useful when you're in immediate need of a result, system administration often requires for information to be provided on a highly repetitive, regular basis. Because human nature dictates that we'll soon fail to follow such a stringent schedule, it's wise to simply automate script execution, redirecting output to either a flatfile to be examined at the administrator's leisure, or better, to an e-mail address. On Unix systems, such automation is often accomplished using the CRON feature. CRON is capable of scheduling a job on a periodic basis, and provides you with the means for determining the timeframe. Because there are several great CRON tutorials available on the Web, I'll forego an introduction, and instead move directly to implementation details. Suppose that I want to receive the results of the timezone-email.php script via e-mail each morning at 8am? Simply open up your crontab, designate the execution parameters, and insert the execution command. The insertion would look like this:

* 8 * * * /usr/local/php/php /home/jason/timezone-email.php

This cron script specifies that at 8am daily the script timezone-mail.php should be passed to the PHP executable. Because the script calls for the output to be sent to my e-mail address, the data arrives almost immediately in my inbox. There's nothing like coffee, donuts, and user resource statistics to start a morning....


The brilliant PHP contributors have done it once again, extending PHP's capabilities in a manner that wasn't expected, yet is now so enormously useful. In the next installment, I'll expand upon this feature, introducing other aspects that you will want to keep in mind as you expand your PHP-based administration repertoire. As always, I welcome questions and comments. Please e-mail me at wj AT!

About the Author

W. Jason Gilmore ( is the Open Source Editorial Director for Apress ( He's the author of the upcoming book, PHP 5 and MySQL: Novice to Pro, due out by Apress in 2004. His work has been featured within many of the computing industry's leading publications, including Linux Magazine, O'Reillynet, Devshed,, and Webreview. Jason is also the author of A Programmer's Introduction to PHP 4.0 (453pp., Apress). Along with colleague Jon Shoberg, he's co-author of "Out in the Open," a monthly column published within Linux magazine.

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Sitemap | Contact Us

Rocket Fuel