Jump to content

[solved] Exec() not functioning as expected


Grabeorama

Recommended Posts

I've been Googling this problem all day now and have nothing to show for it. I'm attempting to run a php script, from a php script using exec. I'm doing this to allow the second php script to run in the background, as it may/may not run for quite some time. This is my code for doing that:

exec("php myScript.php > out.txt 2> err.txt &");

With this code I get two new files (as expected) out.txt and err.txt.out.txt is empty, and err.txt contains:

 php: /opt/lampp/lib/libxml2.so.2: version `LIBXML2_2.9.0' not found (required by php)php: /opt/lampp/lib/libcrypto.so.1.0.0: no version information available (required by php)php: /opt/lampp/lib/libssl.so.1.0.0: no version information available (required by php)

I can run the command "php callingScript.php" (the script containing the exec call) from the command line manually, and it works fine. I thought the problem might be related to permissions for the 'nobody' user run by Apache, but when running the command:

sudo -u nobody php callingScript.php

It works. I've also tried running it with the direct path to php:

/usr/bin/php callingScript.php

Any ideas as to what's going on ie. why I can run it manually but not via localhost using exec()? And thanks for reading.

Edited by Grabeorama
Link to comment
Share on other sites

It might be a user issue, it might be that the scripts are being executed as different users. You can check what the process owner is with this:

$processUser = posix_getpwuid(posix_geteuid());print $processUser['name'];

Link to comment
Share on other sites

Hey thanks for the reply, I created two files: index.php:

$user = posix_getpwuid(posix_geteuid())['name']; print "Executing as {$user}<br />\n";print shell_exec("php hello.php");

and hello.php

print "Hello<br />\n";

Running the index.php from command line works fine, outputting "Executing as <user><br />Hello", as expected

php index.php

Running the same file from localhost/index.php outputs "Executing as Nobody" as expected, but does not output the shell_exec() command. (Presumably because it's running as Nobody) But then when I run the following command:

sudo -u nobody php index.php

I get the same output as through localhost, but it does show the output of the shell_exec().Any ideas as to why?

Edited by Grabeorama
Link to comment
Share on other sites

It would be interesting to also print the user in the other file to see if they are ever different. One issue might be the working directory, where it can't find the other PHP file in one case. The working directory for PHP is set differently in the command line versus other SAPIs.

Link to comment
Share on other sites

How could I print the user of the other file if the shell_exec() isn't running the other file? I've just tried it there, getting this from the command line: php index.php

Index: user
Hello: user
sudo -u nobody php index.php
Index: nobody
Hello: nobody
but from localhost I only get:
Index: nobody
as it's not executing the hello file. The same problem for trying to output their working directories. Is there another way I can check?
Link to comment
Share on other sites

First, make sure that you have all errors enabled. It doesn't sound right that it would just not execute the file and not show any error. error_reporting(E_ALL);ini_set('display_errors', 1); You should also use var_dump with the output from shell_exec to see if it's returning null.

Link to comment
Share on other sites

A return value of null means that an error occurred when it was trying to execute the command, so I guess the point now is to figure out what that error is. I'm not too sure about command line switches on Linux, but if you could redirect stderr to stdout then that might show the error message.

Link to comment
Share on other sites

Well I have redirected the output to stderr (as mentioned in the OP) and got the following error (only for localhost):

php: /opt/lampp/lib/libxml2.so.2: version `LIBXML2_2.9.0' not found (required by php)php: /opt/lampp/lib/libcrypto.so.1.0.0: no version information available (required by php)php: /opt/lampp/lib/libssl.so.1.0.0: no version information available (required by php)

But I can't make heads nor tails of it! EDIT:Is it possible that I have multiple versions of the files mentioned in the errors and that PHP is confused as to which to use?

Edited by Grabeorama
Link to comment
Share on other sites

From looking up error messages like that, it sounds like PHP was compiled on a system with newer versions than what you're running it on. But people also seem to think those aren't necessarily fatal errors. Do those files actually exist on your system? It's interesting that it's using libraries that would be in the lampp directory instead of the system libraries.

Link to comment
Share on other sites

Ok problem solved. I think the system was getting confused with file versions used by PHP, as I had 2 different PHP interpreters installed, one from Lampp and one I installed before Lampp. So by uninstalling PHP and adding the Lampp PHP interpreter to the PATH, things started to work correctly again! Thanks for the help, really appreciate it!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...