Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot run shell command through php exec, but can as user on shell?

I'm trying to get exiftool to work on my dedicated server. The issue is that PHP exec seems to run different than when a command is run as a user. Oddly enough, PHP shows up as the same user I log in with, but it does not behave the same with system commands.

Oddly enough everything works great on my localhost, but not on my server.

So as mentioned, running exiftool commands logged in via ssh is fine.

But running in a php testing script (note I've installed exiftool on each tested directory, and it runs through ssh), nothing is accessible, though it runs as user orangeman...

And it fails

Here is an update - having been on this all day:

On the shell:

-bash-4.1$ which exiftool -a
~/perl5/bin/exiftool
/usr/bin/exiftool
~/perl5/bin/exiftool

In PHP shell_exec('exiftool -a');

/usr/bin/exiftool

And here is what that file links to:

lrwxrwxrwx    1 root root          33 May 15 02:10 exiftool -> /home/orangeman/perl5/bin/exiftool

I've also tried creating symlinks of various sorts, tampering with the main $PATH variable via putenv(); in php ... I'm truly in the dark here. Works on localhost, not on dedicated server.


I've updated this with a bounty - its a serious issue in development.

I'm on a dedicated server, and the problem is as outlined above.


UPDATE Per @gcb suggestion, I was able to print out the error that is occurring when php's exec() function runs the system command with no effect.

PHP

<?php
exec('exiftool 2>&1', $output, $r);
var_dump($output, $r);
?>

Output:

array(2) {
  [0]=>
  string(230) "Can't locate Image/ExifTool.pm in @INC (@INC contains: /bin/lib /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /bin/exiftool line 33."
  [1]=>
  string(59) "BEGIN failed--compilation aborted at /bin/exiftool line 33."
}

UPDATE

@gcb's solution worked. Thank you very much.

like image 647
Orangeman555 Avatar asked Jan 10 '23 14:01

Orangeman555


1 Answers

0) you should look on your error log. usually under /var/log/apache/error

it will have messages such as "access denied" or something else.

1) you are clearly not getting enough output of that command to see any error. so try to run it with exiftool 2>&1. this will redirect stderr to stdout, so errors will appear on the output. not sure if that is relevant or php already does that. you may also want to use passthru instead of exec

2) safe-mode exec dir

your file may be out of your safe mode exec dir. read this up:

http://www.php.net/manual/en/ini.sect.safe-mode.php#ini.safe-mode-exec-dir

3) all else fails, run the command as a login shell, which should have the same scripts loaded as when you login in via ssh. just replace exec with shell_exec

...i'm pretty sure looking at the error log will solve your mistery.

like image 99
gcb Avatar answered Jan 18 '23 14:01

gcb