<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Johan&#039;s Blog &#187; shell</title>
	<atom:link href="http://blog.johan-mares.be/tag/shell/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.johan-mares.be</link>
	<description>A personal blog</description>
	<lastBuildDate>Sun, 24 Oct 2010 15:08:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Running PHP CLI shell scripts</title>
		<link>http://blog.johan-mares.be/ict/php/running-php-shell-scripts/</link>
		<comments>http://blog.johan-mares.be/ict/php/running-php-shell-scripts/#comments</comments>
		<pubDate>Thu, 01 Jan 2009 19:16:27 +0000</pubDate>
		<dc:creator>Johan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[shebang]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://blog.johan-mares.be/?p=71</guid>
		<description><![CDATA[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.]]></description>
			<content:encoded><![CDATA[<p></p><div class="tweetmeme_button" style="float: left; margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.johan-mares.be%2Fict%2Fphp%2Frunning-php-shell-scripts%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.johan-mares.be%2Fict%2Fphp%2Frunning-php-shell-scripts%2F&amp;source=maresjohan&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>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.</p>
<p>Why would you want to use PHP CLI ? Well you don&#8217;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.<span id="more-71"></span></p>
<p>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.</p>
<p>First you&#8217;ll have to make sure that PHP CLI is installed.</p>
<pre>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</pre>
<p>If you see &#8216;cli&#8217; somewhere on the first line, you are ready to go, otherwise you will have to install it first. For example on Ubuntu:</p>
<pre>johan@ubusrv:~$ sudo apt-get install php5-cli</pre>
<p>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 &#8216;/usr/bin/php&#8217;.</p>
<pre>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</pre>
<p>For the <strong>first method</strong> we&#8217;ll use the following script:</p>
<pre>&lt;?php
        echo "First PHP CLI script\n";
        echo exec('ls -l\n') . "\n";
?&gt;</pre>
<p>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.</p>
<pre>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</pre>
<p>For the <strong>second method</strong> we&#8217;ll add a <a title="shebang" href="http://en.wikipedia.org/wiki/Shebang_(Unix)" target="_blank">shebang</a> to the first line of the script:</p>
<pre>#!/usr/bin/php -q
&lt;?php
        echo "First PHP CLI script\n";
        echo exec('ls -l\n') . "\n";
?&gt;</pre>
<p>I have added the &#8216;-q&#8217; in the shebang, because you will find a lot of examples like this, but you don&#8217;t need it. According to the <a title="PHP Manual" href="http://be.php.net/manual/en/features.commandline.php" target="_blank">PHP Manual</a> CLI is started up in quiet mode by default, though the <span class="option">-q</span> and <span class="option">&#8211;no-header</span> switches are kept for compatibility so      that you can use older CGI scripts.<br />
After you&#8217;ve made the file executable, you can now run it like any other shell script.</p>
<pre>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</pre>
<table style="border: medium none; margin: 20px auto; width: auto;" border="0" align="center">
<tbody>
<tr>
<td>My software never has bugs, it just develops random features</td>
</tr>
<tr>
<td align="right"></td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://blog.johan-mares.be/ict/php/running-php-shell-scripts/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: basic
Database Caching 1/21 queries in 0.006 seconds using disk: basic
Object Caching 308/352 objects using disk: basic

Served from: blog.johan-mares.be @ 2012-02-06 06:01:27 -->
