Problem with exec in PHP - php

I am running through a weird problem with exec's in PHP. When I load in the browser the script x.php, the browser hangs and when I run a ps I can see multiple threads being created. The only way to stop them is by restarting apache.
However, instead of running a php script, if I do something like system('ls'), it works fine. So it seems to be problem when a PHP script tries to run another script using exec/system/passthru (I've tried them all).
x.php is defined as following:
<?php
var_dump(system('php -f t.php'));
?>
t.php is defined as following:
<?php
echo 'Hello world';
?>

Take a look at first comment in exec() manual.

Related

Running PHP Script In Powershell keeps opening NotePad ++ rather than executing the PHP script

Okay, I'm at a bit of a loss here.
I'm testing out running PHP scripts from within powershell and it just keeps opening NotePad ++ rather than executing the script. I cannot figure out why this won't work...
I'm using a pretty basic PHP script to test:
<?php
echo 'Hello, World!';
?>
And I'm calling it using the standard way I run .ps1 files:
PS C:\php> c:\phpfiles\test25.php
The execution policy is set to unrestricted... what am I doing wrong?
You should pass the path of the file as an argument to the PHP executable. If (lets say) PHP is installed in c:\php, then you must do:
PS c:\php\php.exe -f c:\phpfiles\test25.php

Issues with exec() on php running a shell script

I'm trying to use a php script for executing a shell script. However I'm having some issues apparently with the web server.
I have a bash script called switch_audio.sh. It basically changes the active audio output of the system.
I also have a script.php that runs the code below:
<?php
echo "It's working";
exec("/var/www/html/switch_audio.sh");
?>
When I execute php script.php it's working fine. However, when I try to run it on the web browser by localhost/script.php I just get as ouput the "echo" part.
I've already tried to:
remove 'exec()' from the disable functions in php.ini;
give permissions for everybody in every folder on this path from "/" to the localhost folder;
Any thoughts about it?
Simple solution: exec() returns a string, but you also have to output it to the user:
<?php
echo "It's working";
echo exec("/var/www/html/switch_audio.sh");
?>
You already said you allowed the shell function on your php.ini configuration but it seems it's not working yet... so maybe:
You didn't restarted webserver service after changes
You have somewhere other statement more restrictive... maybe ini_set in your php files.
As a reminder, be sure that you are doing well on your php.ini file, check it for this kind of statement:
disable_functions=exec,passthru,shell_exec
Maybe you can try instead of exec other similar php function like shell_exec to check if it works. Or maybe is working and not showing anything.

Bash script runs from php command line but not from page

I have a bash script that works as expected from both the shell and the PHP command line, but not when called from a PHP page in Apache (Raspbian). I.e, this works (PHP command line):
>php exec('/var/www/html/scripts/myBashScript.sh');
But this doesn't (index.php):
<?php
exec('/var/www/html/scripts/myBashScript.sh');
?>
No error messages are displayed and I can't see anything relevant in the Apache server logs. As suggested by other responses, I've also tried:
exec('sh /var/www/html/scripts/myBashScript.sh')
exec('./scripts/myBashScript.sh')
exec('sh ./scripts/myBashScript.sh')
Both script file and its containing folder have rwx permissions for the Apache user (www-data). The script is set to executable. Built-in bash commands work as expected from the php file, i.e. this works:
<?php
echo exec('whoami');
?>
What am I missing?
The problem turned out to be related to non-Bash commands in the scripts rather than to the script itself. See comments above. Thank you #AlexanderO'Mara.

Run bash script to create file in PHP

In addition to my previous question, another problem appeared and I decided to make a new question for it:
I am currently calling a php script that than runs a bash script. The php script looks like:
chdir('/home/');
$output = shell_exec('./do.sh');
echo "<pre>$output</pre>";
The do.sh contains:
#! /bin/bash
echo 12;
dd if=/dev/urandom of=test.test bs=1048576 count=2
The problem is following:
When I call ./do.sh from the terminal everything works fine: test.test is created and the ouput is 12
However, when I call it from my php file, the output is 12 aswell, but no file is being created. Since I know almost nothing about bash scripting, I have no idea why this is happening...
Check if PHP safe_mode is enabled. You have to turn it off in your /etc/php.ini file, and obviously check filesystem permissions.

Can't execute PHP script using PHP exec

I am trying to invoke a script which takes several seconds (web services with 3rd party) using the PHP exec call. After much struggling, I reduced this to the classic hello world example. The calling script looks like:
exec('/usr/bin/php /home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &');
When I run this, the output execoutput.txt contains a copy of the invoking script page, not hello world as I expected.
Why can't I get this PHP script to execute using exec? Note that when I change the command to something like ls -l, the output is a directory listing as expected. btw, in case it matters, I did chmod the called script to 755...
Update - I moved the exec call to the end of the calling script and at least now I don't see the calling script executed in the output. Thx to posters and I will try some of these ideas.
Help!
Thanks
Steve
I had this issue also and it turns out this is a bug in php (#11430). The fix is to use php-cli when calling another php script within a php script. So you can still use exec but rather than use php use php-cli when calling it in the browser:
exec("php-cli somescript.php");
This worked for me.
What exec is doing is taking the rightmost command and appending it to your destination. If you have the shebang line in your php script, you shouldn't need to include the binary directive of the php interpreter.
if you just want the script's output, try:
exec('/home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &')
however if you do not want the errors to be in the file, you should redirect the STDERR prior to outputting to the file. Like so:
exec('/home/quote2bi/tmp/helloworld.php 2> /dev/null > /tmp/execoutput.txt')
the above should only output the "Hello World" to the execoutput.
Edit:
Interesting you are getting this behaviour. You stated the command "ls" worked. Try making an alias for this and forward it to a file like so:
alias pexec='php /home/quote2bi/tmp/helloworld.php'
then
exec('pexec > /tmp/execoutput.txt 2>&1 &')
it seems to be a problem with the way exec handles input as opposed to the shell itself.
-John
The problem is with PHP itself, it treats everything as $argv in the script. It doesn´t redirect the output to a file ou to /dev/null.
I faced the same problem some time ago. What I did is to create a runscript.php in /opt/php-bin and then inside this script run what It should be running. Something like this:
$script = $argv[1]
$params = implode(' ', array_slice($argv, 2));
$cmd = "{$script} {$params} > /dev/null &";
$output = array();
$return = 0;
exec("php {$cmd}", $output, $return);
exit((int)$return);
And then you call it using:
exec('/opt/php-bin/runscript.php /path/to/your/script.php arg1 arg2')
It´s the only way I managed to get this working.
To avoid the stated problems of PHP in this area, why not put this in inside a shell script? PHP can then execute the shell script which has all the redirections handled internally.
If you need to dynamically change things, then why not write the shell script and then execute it (and of course, clean up afterwards)?
if you are just simply running a php script one possible way to execute the entire code is to use the include() that will run the php file and output any results. You cannot direct the output to a text file but it should appear in the browser window if you're Hello World php script looks like
<?php echo "Hello World!"; ?>
then it will spit that out in the browser. So your second code would look like
<?php include("helloWorld.php"); echo " PHP ROCKS";?>
resulting in a page that would look like,
Hello world! PHP ROCKS
This runs as if you run the script from browser.
This came across while working on a project on linux platform.
exec('wget http://<url to the php script>)
Hope this helps!!

Categories