run php output as shell script - php

I want to use shell and php together.
First I tried:
shell_exec vs functions is disabled and must be so.
But php does not give me permission to run shell_exec()
So, I gave up and tried to make a .sh, call php,store output of php as sh file and run sh file.
Here is sh code
#!/bin/bash/
php test.php -> running php file/ test.php saves commands in script.sh
sh script.sh -> running the commands
rm script.sh -> removing the commands
But there must be a better way from this file process.
Can I run output of test.php directly in .sh file?
Can I run shell_exec ?
Note: I have root access of the server.

You can pipe (|) the output of PHP to sh to be executed:
$ php -r 'echo "echo \$PATH";' | sh
outputs:
/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/Applications/MAMP/Library/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
I've read some statements using -r for simplicity, but you can read from a file by passing a script to php:
$ php test.php | sh

Related

PHP script will not run bash command

I am having trouble getting my PHP script to run a bash command/script. Here is an export of my PHP script (it is triggered when a HTML form is submitted):
}
chdir('/usr/local/sim/');
shell_exec('killall -9 sim');
shell_exec(./write-sims.sh');
chdir('/var/www/html/');
shell_exec('php -f test.php');
?>
I know that this PHP script is running okay as the 'killall -9 sim' command is executed as expected along with the 'write-sims.sh' script.
I put my bash command inside a PHP script as a test to see if that would work but it doesn't, 'test.php' is as follows:
<?php
shell_exec("find /usr/local/sim/data -maxdepth 1 -type f -name '*.conf' -exec sh -c '/usr/local/sim/build/src/sim --daemon --config {} &' \;");
?>
When I run test.php manually via cli it runs fine however whenever it is called by the first php script it doesn't run.
I have tried switching things around by putting the bash command in a .sh script and calling that from the first php script and also adding the bash command itself inside the first php script but neither work however I don't get any errors in the nginx error log which seems to suggest the script is running okay without errors.
Any idea why this is not running but runs fine when I do it manually? Thanks!

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

Running Linux DAEMON PHP files, does not execute exec, but does when ran from console

I have a php script that works perfectly when ran form command line; including the exec().
The exec I do are calling some .sh scripts.
These .sh script are of type bash
So I call them:
exec('/bin/bash /home/user/soft/bin/mybash.sh > /dev/null');
It works great when I run my php from command line I see all the instructions; works also when use with an alias. but when I it via a daemon:
sudo -u myuser php -f /home/user/bin/serverwait.php eveything that should be done in the .sh are not being done. but the rest of the file is doing what it is suppose to do (it checks for open ports and write to a log file)
I tried looking at my php.ini for cli and all seems right.

Why wget backticks shell command works in PHP CLI and not in a PHP script?

just for fun I am trying to make wget downloads from php cli (/usr/bin/php -a) and it works:
php > `wget -q -nd -O /Users/user/path/to/file.html http:\/\/host.com/some_file.html`;
php > // a HTML file is downloaded and its content is placed into /Users/user/path/to/file.html
However, when I try to do the same thing from a PHP script, it does not work:
<?php
`wget -q -nd -O /Users/user/path/to/file.html http:\/\/host.com/some_file.html`;
// After requesting the script from the browser and after the script is executed, the file doesn't exist on the specified path
I would like to say that the user which executes apache and therefore PHP server side scripting is the same as the user which executes the php command from the command line (so I guess this should not be a problem of permissions).
Why if I use a .php script and call wget inside the script the file is not saved?
Thanks for the attention!
PS: please, do not tell me that I can use curl for such a task, I know I can, I am just curious to know how can I do something similar without using PHP tools, that's it
use the full path to wget, since the daemon doesn't run .bash_profile.
`/opt/local/bin/wget -q -nd -O /Users/user/path/to/file.html http://host.com/some_file.html`;
BTW, there's no need to escape / in shell commands.
The backtick operator runs a shell command and returns the output from the shell command.
In this situation, simply logging (or if you have to, echo-ing) the result will probably reveal the error. E.g:
$result = `wget -q -nd -O /Users/user/path/to/file.html http:\/\/host.com/some_file.html`;
trigger_error($result, E_USER_NOTICE);

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