Ubuntu – Execute a PHP 5 Script From the Command Line

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:

passthru()
exec()
system()


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

 

Leave a Reply

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

*