Running PHP CLI shell scripts

by Johan on January 1, 2009

I already knew how to run PHP scripts from the command line (CLI), although I never really used it. What was new to me was that there are 2 ways of doing this. The first one is by using the php command and the second, and new for me, is by adding a shebang on the first line of your script.

Why would you want to use PHP CLI ? Well you don’t have to learn another programming language like perl or bash to make shell scripts, you can run cron jobs in PHP and you can reuse code from a website for example in your shell script.

Before you read any further, the examples below will work for linux and Mac OS X, but not for Windows. I used Ubuntu 8.04 server and Mac OS X 10.5.6.

First you’ll have to make sure that PHP CLI is installed.

johan@ubusrv:~$ php -v
PHP 5.2.4-2ubuntu5.3 with Suhosin-Patch 0.9.6.2 (cli) (built: Jul 23 2008 06:44:49)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies

If you see ‘cli’ somewhere on the first line, you are ready to go, otherwise you will have to install it first. For example on Ubuntu:

johan@ubusrv:~$ sudo apt-get install php5-cli

It it also useful to know the full path to the PHP binary on your computer. There are several ways of finding out. For Ubuntu and Mac OS X the path is ‘/usr/bin/php’.

johan@ubusrv:~$ which php
/usr/bin/php
johan@ubusrv:~/cli$ whereis php
php: /usr/bin/php /usr/share/php /usr/share/man/man1/php.1.gz
johan@ubusrv:~/cli$ type -a php
php is /usr/bin/php

For the first method we’ll use the following script:

<?php
        echo "First PHP CLI script\n";
        echo exec('ls -l\n') . "\n";
?>

You can either execute it by using the php command or by using the full path of the php binary. In this case it is not necessary to make the file exectuable.

johan@ubusrv:~/cli$ php firstcli.php
First PHP CLI script
-rw-r--r-- 1 1000 1000 71 2009-01-01 19:39 firstcli.php
johan@ubusrv:~/cli$ /usr/bin/php firstcli.php
First PHP CLI script
-rw-r--r-- 1 1000 1000 71 2009-01-01 19:39 firstcli.php

For the second method we’ll add a shebang to the first line of the script:

#!/usr/bin/php -q
<?php
        echo "First PHP CLI script\n";
        echo exec('ls -l\n') . "\n";
?>

I have added the ‘-q’ in the shebang, because you will find a lot of examples like this, but you don’t need it. According to the PHP Manual CLI is started up in quiet mode by default, though the -q and –no-header switches are kept for compatibility so that you can use older CGI scripts.
After you’ve made the file executable, you can now run it like any other shell script.

johan@ubusrv:~/cli$ chmod +x firstcli.php
johan@ubusrv:~/cli$ ./firstcli.php
First PHP CLI script
-rwxr-xr-x 1 1000 1000 89 2009-01-01 19:56 firstcli.php
johan@ubusrv:~/cli$ /home/johan/cli/firstcli.php
First PHP CLI script
-rwxr-xr-x 1 1000 1000 89 2009-01-01 19:56 firstcli.php
My software never has bugs, it just develops random features

{ 15 comments… read them below or add one }

Pascal January 4, 2009 at 12:43

Not only can you use PHP to write command line scripts, but you could also use it to write desktop applications; see http://gtk.php.net/

“PHP-GTK is an extension for the PHP programming language that implements language bindings for GTK+.”

(ps: your capcha words are very difficult to read)

Reply

Ivan Jovanovic January 4, 2009 at 21:58

Using PHP CLI for implementation of application cron tools is regular thing in complex PHP apps. When having application that has well defined model layer you can implement tools that operate on the model from CLI scripts and reuse most of the model. Also when implementing models that should work only with PHP CLI, they should also be the part of the model and CLI script should be kept simple as possible. It is very easy to make hard to read PHP CLI scripts.

Thanks for great post.
Ivan

Reply

Cristiano January 5, 2009 at 15:29

Great overview. Good Job.

Reply

Mark Cole January 5, 2009 at 17:26

My friend Ricki created a brilliant IRC Services package using the PHP CLI. http://acorairc.svn.sourceforge.net

It’s as good as Anope and other IRC Services. It’s great to see PHP doing these kind of things as not long ago it was restricted to web based scripts.

Reply

Angelo Mandato January 6, 2009 at 16:26

I have about a couple dozen php command line scripts that run daily. I’ve learned a lot over the past few years on how to integrate a command line app with an existing web site. I just wrote a blog post on some of the things I do: http://www.compiledweekly.com/2009/01/06/integrating-php-command-line-scripts-with-existing-web-projects/

Basically, you can do a lot with the command line interface in PHP. What I didn’t mention is how you can control the memory size limit and execution time from the command line as well. This way your command line app can take as long as it wants and have the amount of memory it needs. This way you don have to put these kinds of settings in the php.ini, which would be a bad idea anyway.

Thanks for the inspiring post. :-)

Reply

Mistajam January 6, 2009 at 16:33

Mark Cole :
My friend Ricki created a brilliant IRC Services package using the PHP CLI. http://acorairc.svn.sourceforge.net
It’s as good as Anope and other IRC Services. It’s great to see PHP doing these kind of things as not long ago it was restricted to web based scripts.

Yeh i saw that service the other day, its proper epic!

Reply

Kevin van Zonneveld January 6, 2009 at 18:59

Cronjobs run periodically at a certain interval. This can cause overlap, and sometimes you need to know about the state of a previous run.

In these cases you can make your PHP CLI script act like a service (daemon), running indefinitely in the background (just like apache or mysql).

I’ve wrapped the required code in the following class:
http://pear.php.net/package/System_Daemon

Reply

Ajai Khattri January 7, 2009 at 0:21

Nothing new about any of this – the shebang is the ‘standard’ way to run scripts on a Unix environment.

Reply

Yandex June 10, 2009 at 21:53

It was very interesting for me, as I beginner in scripting (sorry for my english).

Reply

varisha July 23, 2009 at 8:21

To learn another programming language like perl or bash to make shell scripts, you can run cron jobs in PHP and you can reuse code from a website for example in your shell script. When having application that has well defined model layer you can implement tools that operate on the model from CLI scripts and reuse most of the model. Also when implementing models that should work only with PHP CLI, they should also be the part of the model and CLI script should be kept simple as possible.

Reply

Ved September 21, 2009 at 18:38

Helped a lot. Kudos to you!

Reply

ahmed May 18, 2010 at 10:40

good job,

Reply

Roberto December 23, 2011 at 17:09

Thank you Johan, this article helped me a lot to understand how to run PHP CLI shell scripts.

Reply

Emmanuel June 11, 2012 at 19:11

Thanks a lot with the article.

Reply

Carsten October 30, 2012 at 13:35

Thank you for helping me to write my first PHP shell script ;-)

Reply

Leave a Comment

{ 5 trackbacks }

Previous post:

Next post: