Maintaining your Server with Command Line PHP

Share this article

More and more in the Information Technology field are we asked to put on various hats and fill multiple roles in our daily jobs. Not only are many of us given the task to create and maintain a web site, but also to maintain the server its hosted on. Usually when maintaining a server you need to know how to write scripts and small programs to automate tasks. A lot of these scripts are written in Perl, Bash, or some other scripting language. Now I’m not against learning new programming languages at all, but did you know that not only can you use PHP to write great web applications, but you can use PHP from the command line as well? You can use the PHP that you already know and love to keep that server running great, all from the command line! In this article we’ll look at the advantages of using PHP CLI. I’ll show you how to test PHP’s Command Line Interface / Interpreter (CLI) on your server, and then we’ll look at some of the options available for PHP CLI including the interactive shell and how to create executable scripts. Finally, I’ll give you a couple of examples of scripts to use to maintain your server written in PHP. One of the biggest advantages of using PHP CLI instead of some other scripting language is the ability to reuse code from previous projects in your script. Whether it be a database class or a function that you created or maybe a file parsing program, with PHP CLI you don’t have to rewrite any of your code. You can also automate many tasks written with PHP CLI with CRON. If you have never used CRON before, it’s a daemon program that runs specified scripts at specific times on your server. Need to generate a report for the sales department on Mondays, Wednesdays, and Fridays? Write a PHP script to do it, throw it into a CRON job, and sit back and relax while PHP and CRON do the work for you.

Running PHP CLI Scripts

If you are running a recent version of PHP, chances are you already have PHP CLI installed on your system. If the examples in this article don’t work for you, then you’ll probably need to recompile PHP with the --enable-cli option or reinstall a PHP package that contains that option. The easiest way to test to see if everything is working is to open up a new terminal window or SSH session to your server or computer and from the command line type in php -v.
Stephens-Laptop:~ sthorpe$ php -v
PHP 5.3.8 (cli) (built: Dec  5 2011 21:24:09)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
Let’s try a quick example where we will run an actual PHP file from the command line. Create a file called test.php with the following code using your favorite editor:
$str = "SitePoint is the best!";
echo $str . "n";
To execute the script, all you have to do is type in php test.php and you’ll see the output on the command line.
Stephens-Laptop:~ sthorpe$ php test.php
SitePoint is the best!
A more preferred method of running PHP scripts on the command-line is to change the permissions mode of the script and place #!/usr/bin/php at the top of the file before the opening <?php tag (your path to PHP may be different depending on your system’s configuration). To accomplish this, you’ll use the chmod command on UNIX-based systems.
$filepath = exec("pwd");
echo "You are in the $filepath directory.n";
Stephens-Laptop:~ sthorpe$ chmod +x pwd.php
Stephens-Laptop:~ sthorpe$ ./pwd.php
You are in the ~ directory.

CLI Options and the Interactive Shell

I’ve already shown you one of PHP’s command-line options earlier, -v which gives you the version number of PHP installed on your system, but there are more options available. For example, if you wanted to share the code from a script you wrote with someone as web page and have it syntax highlighted for better readability, you can use the -s option. PHP will output the appropriate HTML code.
Stephens-Laptop:~ sthorpe$ php -s test.php < test.html
When viewed in a web browser, the resulting test.html presents the highlighted code:

Another helpful option is -l
to check your scripts for any syntax errors. If no errors are found in the file, PHP will report back there were no syntax errors. Otherwise PHP will report the error it found and on which line it occurred.
Stephens-Laptop:~ sthorpe$ php -l broken.php
PHP Parse error:  syntax error, unexpected T_VARIABLE in broken.php on line 3
Errors parsing broken.php
In addition to the numerous options available, with PHP CLI you can test out ideas in PHP code right from the shell without having to create any files at all! This is useful when you want to see what the results of a function might be. To run PHP’s interactive shell use -a. PHP will report back “Interactive shell” and the prompt will change to php >. From this point forward you can just type your code and PHP will execute it immediately. To exit the shell, you can either press CTRL + D or call exit.
Stephens-Laptop:~ sthorpe$ php -a
Interactive shell

php > echo 2 + 2;
php > exit;

Getting Input

With CLI programming, you have two options for input functionality. The first option is to pass arguments to your script directly from the command line. The second option is reading the input from the keyboard (standard input) from the user running the script. To accept arguments into your script, you’ll use the predefined superglobal variables $_SERVER["argc"] and $_SERVER["arvc"]. Create a file called arguments.php containing the code below:
echo "There are " . $_SERVER["argc"] . " arguments:n";
foreach($_SERVER["argv"] $arv) {
   echo "t" . $arv . "n";
$_SERVER["argc"] contains the number of arguments that were passed to the script, and $_SERVER["argv"] is an array containing all the values of those arguments. You will always have at least one argument since the file name itself is considered the first argument. Make the file executable and pass in a few arguments with a space in between each one. The script will display the total number of arguments and list each one.
Stephens-Laptop:~ sthorpe$ php arguments.php SitePoint Rocks
There are 3 arguments
Accepting input from standard input lets you create interactive scripts which can prompt for specific information. Create a file called keyboard.php with the following code:
echo "Hi There! what is your favorite web site?n";
$site = fread(STDIN, 80);
$site = trim($site);

if ($site != "SitePoint") {
   echo "Are you sure that one is your favorite?n";
else {
   echo "I knew it! Me too!n";
In this example we are using the fread() function just as we would to read in a file, but in this case the handle is the PHP predefined constant STDIN. Once again make the file executable and run the file; hopefully your output will be the same as mine.
Stephens-Laptop:~ sthorpe$ php keyboard.php
Hi There! what is your favorite web site?
I knew it! Me too!

Some PHP CLI Examples

When monitoring a server, one thing worth keeping track of is your available disk space. These days hard drives are so huge that you wouldn’t think that there was a way you could possibly fill up all that space, but trust me… it can happen when you least expect it. Maybe your log rotation process broke and you didn’t realize it, or your temp directory grew out of control. Here’s an example you can use to keep an eye on your server’s disk space:
// config options
$disk = "/dev/disk0s2";
$threshold = 90;
$emailAddr = "";
$emailName = "Your Name Here";

exec("df -h " . $disk, $output);
foreach($output as $line){
    $info = strstr($line, $disk);
    if ($info != "") {
$pos = stripos($info, "%");
$pos = $pos - 3;
$used = substr($info, $pos, 3);

if ($used >= $threshold) {
    mail($emailAddr, "System HD Notification", "Main disk is at " . $used . "%" , "From: $emailName");
The function exec() executes a command in the command line and accepts two parameters: the first is the actual command (in this case df which displays disk space information), and the second is an array reference that is filled with the command’s output. The $disk variable holds the path of the disk you want to monitor (in my case it’s /dev/disk0s2). The df command outputs various other data than just disk space used, so we’ll only be looking for specific information from the $output array. The code continues by looping through the array and using the strstr() function to find the desired disk and break out of the loop once it’s been found. I strip out the part of the string that contains the percentage of disk space left with using substr() and compare it to a predetermined value, in this case if the hard drive usage reaches 90% or more. If so, the script sends an email alerting me with the information. You could have this quick script run as a cron job every 30 minutes or so and it should give you some peace of mind. Another common task when maintaining a server is to insure that you have a constant backup of files and folders. If some sort of version control of back-up service isn’t available for whatever reason, this next script can be used to back up some folders and a database and send them to an off-site SFTP server.
// remote SFTP connection credentials
$sftpServerIP = "";
$sftpUsername  = "sftpuser";
$sftpPassword = "hushhush";
$sftpTarget   = "/home/sftpuser";

// mail notification config
$emailAddr = "";
$emailName = "Your Name Here";

// MySQL database connection credentials
$mysqlUsername = "dbuser";
$mysqlPassword = "secret";
$mysqlDatabase = "myDatabase";

// list of directories and files to back up
$files = array(

// create temporary directory and copy files/directories to it
$tmpFolder = "/tmp/" . uniqid();
mkdir($tmpFolder, 0700);
foreach ($files as $f) {
    exec("cp -r $f $tmpFolder/");

// dump the database content
exec("mysqldump -u $mysqlUsername -p$mysqlPassword $mysqlDatabase > $tmpFolder/backup.sql");

// compress the backup
exec("tar -czf $tmpFolder.tgz $tmpFolder/*");

// establish the sftp connection
$session = ssh2_connect($sftpServerIP, 22);
if ($session === false) {
    mail($emailAddr, "System Backup", "Could not connect to SFTP server", "From: $emailName");
$result = ssh2_auth_password($session, $sftpUsername, $sftpPassword);
if ($result === false) {
    mail($emailAddr, "System Backup", "Could not authenticate to SFTP server", "From: $emailName");
$sftp = ssh2_sftp($session);
if ($sftp === false) {
    mail($emailAddr, "System Backup", "Could not initialize SFTP subsystem", "From: $emailName");

// transfer the backup file
$date = date("D");
$upload = file_put_contents(
    "ssh2.sftp://" . $sftp . $sftpTargetDir . "/backup-$date.tgz", 

if ($upload) {
    mail($emailAddr, "System Backup", "Your files has been backed up successfully", "From: $emailName");
else {
    mail($emailAddr, "System Backup", "Something went wrong with the upload of the backup", "From: $emailName");

// clean up the local temp backups
exec("rm -r $tmpFolder $tmpFolder.tgz");
Again we use the exec() function to execute other programs and commands from within our PHP script, such as mysqldump to dump the database content and tar to compress the backup folder into an archive. into the variable we defined before. Connecting to the SFTP server uses PHP’s SSH2 extension, so you’ll need to have that extension installed. A good write up can be found in Kevin van Zonneveld’s blog. The code shouldn’t be too difficult to modify if you want to use a pure solution like phpseclib or even an older (and insecure) protocol like FTP. Regardless of your approach, the sample highlights some of the benefits of scripting such tasks with PHP.


In this article we explored using PHP from the command line to write server maintenance scripts. I talked about the advantages of using PHP CLI, including the ability to automate your scripts. We went over some neat options available, like the ability to test your scripts for syntax errors and using interactive mode. We also learned about the different input functionality available, including accepting input straight from the keyboard. I then gave you two real world examples of scripts to use to maintain your own servers which included a hard drive monitoring script and a backup script. I hope this article has given you some ideas and knowledge that will help you in the future when you are given the task to maintain that server, and I hope you can also use these CLI example scripts as a bases to create your own maintenance scripts. Image via Chengyuan Yang / Shutterstock

Frequently Asked Questions (FAQs) about Maintaining Your Server with Command Line PHP

How can I install PHP for command line use on Windows?

To install PHP for command line use on Windows, you first need to download the PHP Windows installer from the official PHP website. Once downloaded, run the installer and follow the on-screen instructions. During the installation process, ensure that you select the option to install PHP for command line use. After installation, you can verify the installation by opening a command prompt and typing ‘php -v’. This should display the installed PHP version.

What are some common PHP command line options?

PHP command line provides several options that can be used to customize the execution of your scripts. Some common options include ‘-f’ to parse and execute a file, ‘-i’ to display PHP information, ‘-l’ to perform a syntax check of the given PHP code, and ‘-r’ to run PHP code in command line mode.

How can I implement a maintenance mode on my website using PHP?

Implementing a maintenance mode on your website using PHP can be done by creating a .maintenance file in your website’s root directory. In your index.php file, you can check for the existence of this file and if it exists, display a maintenance message instead of executing the normal code. Remember to remove the .maintenance file once maintenance is complete to resume normal operation of your website.

What versions of PHP are currently supported?

The PHP Group provides support for the latest three PHP versions. As of now, these are PHP 7.4, PHP 8.0, and PHP 8.1. Each of these versions receives active support for two years from their initial release. After this period, they receive an additional year of critical security updates.

How can I update my PHP version from the command line?

Updating your PHP version from the command line depends on the system you are using. On a Unix-like system, you can use the package manager to update PHP. For example, on a system using apt, you can use ‘sudo apt-get update’ followed by ‘sudo apt-get upgrade php’. On Windows, you will need to download the new version from the PHP website and update your PATH environment variable to include the new version.

How can I run a PHP script from the command line?

To run a PHP script from the command line, navigate to the directory containing the script using the ‘cd’ command. Then, type ‘php’ followed by the name of the script. For example, if your script is named ‘script.php’, you would type ‘php script.php’.

Can I use PHP for system administration tasks?

Yes, PHP can be used for system administration tasks. PHP’s command line interface allows you to write scripts that can interact with the system, similar to scripts written in languages like Bash or Python. This includes tasks like file manipulation, process control, and even network communication.

How can I handle command line arguments in PHP?

Command line arguments in PHP can be accessed through the $argv array. The first element of this array, $argv[0], is always the name of the script. The following elements, $argv[1], $argv[2], etc., contain the arguments passed to the script.

Can I use PHP’s command line interface to interact with databases?

Yes, PHP’s command line interface can be used to interact with databases. This can be done using PHP’s database extensions, such as PDO or MySQLi. You can use these extensions to connect to a database, execute queries, and retrieve results, all from a command line script.

How can I handle errors in PHP command line scripts?

Errors in PHP command line scripts can be handled using the same techniques as in web scripts. This includes using try-catch blocks to catch exceptions, checking the return values of functions, and using the error_reporting function to control which errors are reported.

Stephen ThorpeStephen Thorpe
View Author

Stephen Thorpe is originally from London but now living in Tennessee. He works at an Internet and Telephone company as an applications developer primarily using PHP and MySQL.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form