How can I launch php script from console - php

How can I launch a php script from Linux console?
For example, I have myscript.php file. I should be able to launch it like myscript from console (I can create and use any other scripts)
Also, I should be able to send parameters to it like myscript dosome [-n <count>]. Can I do this?

You could find it with very simple Google search.
So you should install PHP-CLI first. For example:
sudo apt-get install php5-cli
After that you should write some code to your php file:
nano example.php
and write
<?php phpinfo(); ?>
For run this script you should give permission:
sudo chmod 755 example.php
And can run it as:
./example.php

To run php script from shell with argumets you must create bash script, name it myscript and add this code (change php to your php installation dir)
"php" myscript.php $*
And add script folder to Linux PATH
$ export PATH=$PATH:/path/to/folder/
Then you can access the script with myscript arg1 -n 20. You can access arguments via $argv variable in php script

Related

PHP exec on local script

Hello i have a PHP script, and its added to cron, it is possible to execute from this script shell command (with exec() or something) without enabling it on php.ini? I don't want to enable exec on my site
It's called PHP CLI, check here
Usually when you install php, there's option to install php_cli too.
So long you can run php on shell prompt, then it can work.
Open bash (or other shell), try this:
php -v
If the version printed, then it's working.
Then you can
php -f phpfile
or put
#!/usr/bin/php
At the beginning of your php file as a line, and chmod +x file.php, and then
./file.php
#or
/path/to/file.php
to run it.
(Note /usr/bin/php is the usual place of php executable, it might change, eg in unix is ually /bin/php. Use whereis php to check its place.)

run php file from shell script in openwrt

I am trying to run a php file from shell script file or terminal in open-wrt platform. I have executed php files in crontab and those are running perfectly. i need to run a php file without putting it into crontab.I am trying it with the following command
chmod 777 /www/api/*
cd /www/api
php myphp.php
but it showing -ash: php: not found
I have also try it putting the following command on top of the script
#!/usr/bin/php
but it is not working. i could not figure out the problem!!!
You must install php-cli package, after you can run the script by
php-cli script.php

execute php script from command line on ubuntu

I can run facebook.php script from ssh below:
cd /var/www/
php facebook.php
But I want to run to script 1 line command because i want to use it on cron. Like this:
php /var/www/facebook.php
I tried other commands on ssh but dont worked. Only first command is worked for me
if /usr/bin/php /var/www/facebook.php is not working, it might a case of your php path is different then /usr/bin/php.

Run GruntJs task from php exec function

I would like to perform some grunt task by running command from php exec function.
I've already:
Install grunt globally: npm -g install grunt-cli
Setup apache and windows to allow running CLI from php (I use
wampserver)
I ran this script from php:
<?php
exec('grunt --gruntfile Gruntfile.js');`
And I found in apache log that grunt command is not found whereas I can run this command from CLI and grunt is in my env path.
So I've decided to create a batch file that contains that script and launch the batch instead of the grunt command directly.
<?php
exec ('gruntTask.bat'); // I know that it can execute script because I can
// create folder if I put mkdir command in gruntTask.bat`
gruntTask.bat contains:
grunt --gruntfile Gruntfile.js
But why it couldn't execute this script? Have I missed something?
Need helps guys,
Regards,

CLI php run php in command prompt

I have installed PHP CLI to execute php commands from console.
I have installed PHP CLI using this command -
sudo apt-get install php5-cli
When I run this
$vr=3; echo $vr;
Result :-
=3: command not found
If I run echo "test";
Result :- test
displays..
Can anyone tell why "command not found" displays..
The "echo "test" line is working because echo is a bash command.
You have to write your own php script, the run it by command line like this:
$ php myscript.php
In alternative, you can run php from your command line, then directly write or paste your script.
Then press CTRL+D to run it. Remember the at the beginning and at the end.
As third option, you can write a php script, putting in the first line this code:
#!/usr/bin/php
Obviously the php executable path must match the one in your system.
This way, you can chmod +x the script, then run it directly like this:
$ ./myscript.php
The fourth option is the interactive shell:
$ php -a
Interactive shell
php > echo 5+8;
13
[$ in front of commands means a command run by user]
You are entering PHP code into the Unix shell (e.g. bash). The Unix shell does not understand PHP code, so you have to run php first.
To run your PHP code from the command line:
$ php -r '$vr=3; echo $vr, "\n";'
3
To run your PHP code from the PHP interactive shell (which may or may not be compiled into PHP):
$ php -a
Interactive shell
php > $vr=3; echo $vr, "\n";
3
php >
(Hit Ctrl+D or type exit to get out of the PHP shell.)
To run your PHP code from a file named prog.php (which contains <?php before the code):
$ php prog.php
3
It seems you want something like this: http://www.php.net/manual/en/features.commandline.interactive.php
This gives you an interactive mode, where you can type PHP code and have it executed directly.

Categories