Page 1 of 1

Command Works from Linux Prompt, But Not via Shell_Exec()?

Posted: Sun Dec 23, 2007 12:10 pm
by Vik
I have a CGI app that is called from my PHP code. It works fine on my development system (Mac OS X 10.5, PPC, PHP 5).

I'm now moving to a hosted server. On this server, for some reason I have to run the following command in order to get the CGI to be able to access a shared library:

Code: Select all

export LD_LIBRARY_PATH=/home/vik/domains/myURL.com/public_html/cgi-bin/cgi_folder:$LD_LIBRARY_PATH
When I run this from the command line, everything works fine.

But when I run "export LD_LIBRARY_PATH" from my PHP app, using shell_exec, it doesn't seem to work.

How can I fix this?

Thanks very much in advance to all for any info.

Posted: Sun Dec 23, 2007 3:14 pm
by John Cartwright
Output? Errors? Does the user running as php have permission to run the command?

Posted: Sun Dec 23, 2007 4:10 pm
by Vik
Thanks very much for your feedback.

The value returned by the shell_exec command is an empty string. No errors are echoed to the screen. PHP is running with safe mode off.

Code: Select all

Does the user running as php have permission to run the command?
I don't know yet. How can I check to find this out?

Posted: Sun Dec 23, 2007 6:52 pm
by s.dot
Probably get_current_user() or getmyuid()

Generally PHP runs as 'nobody'. Depending on your server config, you may or may not have permission to run shell commands. If you're on a shared server, most likely you're not allowed to.

Posted: Mon Dec 24, 2007 1:53 pm
by Vik
I ran get_current_user, and my login name came back. I then ran:

echo "<br /><br />output of shell_exec('ls -lart'):<br />";

...and got a directory listing.

What could I be missing?

Posted: Mon Dec 24, 2007 3:35 pm
by Jenk
Use shell_exec to execute:

Code: Select all

export LD_LIBRARY_PATH=/home/vik/domains/myURL.com/public_html/cgi-bin/cgi_folder:$LD_LIBRARY_PATH 2>&1
that will show error output, should there be any.

Posted: Mon Dec 24, 2007 4:00 pm
by Vik
Hey, this is providing useful new info.

This code (which is what I was trying):

$cmd = "export LD_LIBRARY_PATH=/home/vik/domains/myURL.com/public_html/cgi-bin/cgi_folder:$LD_LIBRARY_PATH 2>&1";
echo "-->" .shell_exec($cmd) . "<--";


...produced this error:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: LD_LIBRARY_PATH

Filename: controllers/my_food_plan.php

Line Number: 1278
--><--

So at least to start, I may need to rewrite the "export LD_LIBRARY_PATH" command somehow to work with the shell_exec() command.

Should I start a new thread about how to rewrite "export LD_LIBRARY_PATH" for use with shell_exec, or may I ask how to do that in this thread?


UPDATE (MOMENTS LATER): Oh wait-- I used double quotes, so PHP tried to use $$LD_LIBRARY_PATH as a variable... let me try it with single quotes...

This time I just got:

--><--


No error message -- what could that mean?

Posted: Mon Dec 24, 2007 5:54 pm
by Jenk
Use single quotes or escape the $ character, PHP is interpreting $LD_LIBRARY_PATH as a PHP variable and not a literal string.

Posted: Mon Dec 24, 2007 7:34 pm
by Vik
You're quite right -- let me try it with single quotes...

This time I just got:

--><--


No error message -- what could that mean?

Posted: Wed Dec 26, 2007 10:53 am
by Vik
The web host got everything working by moving a shared library file to a directory where the CGI was looking for it. I have no idea why the path file had to be moved, given that the app could be run from the SSH command line, but, the situation has been taken care of .

Thanks to all here for your input.