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

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
Vik
Forum Newbie
Posts: 13
Joined: Sun May 23, 2004 1:12 pm
Location: Los Angeles, CA

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

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Output? Errors? Does the user running as php have permission to run the command?
Vik
Forum Newbie
Posts: 13
Joined: Sun May 23, 2004 1:12 pm
Location: Los Angeles, CA

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Vik
Forum Newbie
Posts: 13
Joined: Sun May 23, 2004 1:12 pm
Location: Los Angeles, CA

Post 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?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
Vik
Forum Newbie
Posts: 13
Joined: Sun May 23, 2004 1:12 pm
Location: Los Angeles, CA

Post 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?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Use single quotes or escape the $ character, PHP is interpreting $LD_LIBRARY_PATH as a PHP variable and not a literal string.
Vik
Forum Newbie
Posts: 13
Joined: Sun May 23, 2004 1:12 pm
Location: Los Angeles, CA

Post 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?
Vik
Forum Newbie
Posts: 13
Joined: Sun May 23, 2004 1:12 pm
Location: Los Angeles, CA

Post 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.
Post Reply