PHP exec on local script - php

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.)

Related

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

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.

Is #!/usr/bin/env required to run PHP from command line?

Often times when I see PHP that is meant to be ran from the command line, it will have this line #!/usr/bin/env php at the top of the file like this...
#!/usr/bin/env php
<?php
// code
?>
I was wanting to know if this is meant just for when the file is ran on a Linux/Unix system or is needed for running on Windows as well?
The shebang line is required for auto-detection of the type of script. It enables this sort of usage:
[pfisher ~]$ chmod +x run-me.php
[pfisher ~]$ ./run-me.php
That line is not needed if you pass the filename as an argument to the php interpreter, like so:
[pfisher ~]$ php run-me.php
Edit: replace "hashbang" with shebang.
No it's not, you can directly use
#!/path/to/php
Running php (or anything else) through the env utility is a weak security measure. Dpending on the platform, will "fix" PATH, LIB, and other environment variables according to various config files and potentially remove some of the dangerous values in there (e.g. env on HPUX).
It is also to limit the scope of shell-expansions on certain environments. (See man 1 env on Linux).
the magic belongs to the executable flag (chmod +x FILE)
the shebang: and if it exists with that version you may expect
/usr/bin/env cliVersion -> /usr/bin/env php
php -v # tells you which version you have by default as: 'cli' in a shell
alternativly use e.g:
php8.0 /to/php/script.php
to run it without a shebang at the first line of the script.
(it will work even if it stays there but check the real php version on execution if important for you)
"standard"? over the last 10 years is /usr/bin/env depending on what version you set to be the default on your system:
debian systems (debian,unbuntu,kubuntu...): #root: update-alternatives --config php will guide you

Bash script to run php script

I have a php script that I want to be run using a bash script, so I can use Cron to run the php script every minute or so.
As far as I'm aware I need to create the bash script to handle the php script which will then allow me to use the Cron tool/timer.
So far I was told I need to put:
#!/pathtoscript/testphp.php
at the start of my php script. Im not sure what to do from here...
Any advice? Thanks.
If you have PHP installed as a command line tool (try issuing php to the terminal and see if it works), your shebang (#!) line needs to look like this:
#!/usr/bin/php
Put that at the top of your script, make it executable (chmod +x myscript.php), and make a Cron job to execute that script (same way you'd execute a bash script).
You can also use php myscript.php.
Sometimes PHP is placed in non standard location so it's probably better first locate it and then try to execute.
#!/usr/bin/env bash
PHP=`which php`
$PHP /path/to/php/file.php
A previous poster said..
If you have PHP installed as a command line tool… your shebang (#!) line needs to look like this: #!/usr/bin/php
While this could be true… just because you can type in php does NOT necessarily mean that's where php is going to be... /usr/bin/php is A common location… but as with any shebang… it needs to be tailored to YOUR env.
a quick way to find out WHERE YOUR particular executable is located on your $PATH, try..
➜which -a php ENTER, which for me looks like..
php is /usr/local/php5/bin/php
php is /usr/bin/php
php is /usr/local/bin/php
php is /Library/WebServer/CGI-Executables/php
The first one is the default i'd get if I just typed in php at a command prompt… but I can use any of them in a shebang, or directly… You can also combine the executable name with env, as is often seen, but I don't really know much about / trust that. XOXO.
You just need to set :
/usr/bin/php path_to_your_php_file
in your crontab.
I'm pretty sure something like this is what you are looking for:
#!/bin/sh
php /pathToScript/script.php
Save that with your desired script name (such as runPHP.sh) and give it execution rights, then you can use it however you want.
Edit: You might as well not use a bash script at all and just add the "php ..." command to the crontab, if I'm not mistaken.
Good luck!
The bash script should be something like this:
#!/bin/bash
/usr/bin/php /path/to/php/file.php
You need the php executable (usually found in /usr/bin) and the path of the php script to be ran. Now you only have to put this bash script on crontab and you're done!
a quick way to find out WHERE YOUR particular executable is located on your $PATH, try.
Even quicker way to find out where php is ...
whereis php
I'm running debian and above command showing me
php: /usr/bin/php /usr/share/php /usr/share/man/man1/php.1.gz
Hope that helps.
If you don't do anything in your bash script than run the php one, you could simply run the php script from cron with a command like /usr/bin/php /path/to/your/file.php.
I found php-cgi on my server. And its on environment path so I was able to run from anywhere. I executed succesfuly file.php in my bash script.
#!/bin/bash
php-cgi ../path/file.php
And the script returned this after php script was executed:
X-Powered-By: PHP/7.1.1
Content-type: text/html; charset=UTF-8
done!
By the way, check first if it works by checking the version issuing the command php-cgi -v
Create file.php with first line in files: file.php(#!/bin/php) file.sh(#!/bin/bash).
Check installed php.Run command in terminal:
which php
If set there will be an answer:
/usr/bin/php
Run file.php with command:
php file.php
if the file has started then you can write this command to file.sh:
#!/bin/bash
run_php=`php file.php`
echo $run_php
Be careful ' and ` different!!!

linux - running php script from command line when php is installed as apache module

Normally when I want to run a php script from the command line I just create a php page, add a shebang pointing to the php binary, then ./file.php to run it. Since I have php installed as an apache module, I'm not even sure what my shebang should look like. Any ideas?
The CLI version of PHP had been part of the default installation since 4.3 and has to be explicitly turned off when PHP is being built. If you have access to the command line try
$ php -v
If you don't get a command not found error then you should be ready to go.
To actually run a php file from the command line do this:
$ php -f file.php
If it's just an Apache module, I don't think you can do it… At least, not without using a script like this:
$ cat run_php_with_apache
#!/bin/sh
cp "$1" /var/www/
curl "http://localhost/`basename "$1"`"
rm "/var/www/`basename "$1"`"

Categories