Execute a PHP 5 Script From the Command Line in Ubuntu Linux

To execute a PHP script from the command line in Ubuntu you must have php5-cli installed. (CLI stands for “Command Line Interface”.)

sudo apt-get install php5-cli

Once cli is installed, you can execute your script like so:

php5 ./myScript.php

Inside the script you can run other commands using your choice of the following PHP functions:

Execute Directly
To execute your script directly, you’ll have to specify the script interpreter on the first line of your script. (AKA a she-bang.)

#!/usr/bin/php5
<?php 
// myScript.php 
echo 'hello world!'.PHP_EOL; 
?>

Make your script executable,

chmod +x myScript.php

and now you can execute it at your convenience.

./myScript.php

This entry was posted in Linux, System Administration and tagged , , , . Bookmark the permalink.

One Response to Execute a PHP 5 Script From the Command Line in Ubuntu Linux

  1. somesh says:

    thanx a lot

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>