exec failing to run imagemagick convert command

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cytryn
Forum Newbie
Posts: 6
Joined: Sat Jul 18, 2009 1:29 am

exec failing to run imagemagick convert command

Post by cytryn »

Hi,

I'm trying to get a php-based LaTeX renderer working and have run into a snag I can't figure out. The script generates a .tex file, typesets it to .ps and then tries to call "convert" to generate the desired .png file. This last step is causing me trouble. The relevant piece looks essentially like this (in the script, the string is generated from variables):

Code: Select all

 
$command = "/usr/local/bin/convert -density 120 -trim -transparent \"#FFFFFF\" filename.ps filename.png";
$status_code = exec($command);
 
I tried adding an "echo $command;" and cut-and-paste the result from the browser to a command line; the convert worked so the command syntax is put together ok. It just doesn't seem to want to do it through exec. Permissions on convert are rwxr-x-r-x so I don't think that's a problem; a similarly structured call to latex (same permissions) worked fine. I also tried removing the -transparent option in case there was some subtle quote-escaping issue but that didn't help. exec('whoami'), exec('pwd'), the latex and dvips commands all worked fine.

Any suggestions? I'm at a loss.

Eric
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: exec failing to run imagemagick convert command

Post by prometheuzz »

Did you manually execute it on the same location and with the same user the PHP script is run under?
cytryn
Forum Newbie
Posts: 6
Joined: Sat Jul 18, 2009 1:29 am

Re: exec failing to run imagemagick convert command

Post by cytryn »

I tried to but I'm not sure exactly how. When I added the exec('whoami'), it gave me "www" - that would be the user, right? I tried to sudo as www but don't know what the password would be for that user. This is all on a mac, btw. How do I execute manually as www?
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: exec failing to run imagemagick convert command

Post by prometheuzz »

cytryn wrote:I tried to but I'm not sure exactly how. When I added the exec('whoami'), it gave me "www" - that would be the user, right? I tried to sudo as www but don't know what the password would be for that user. This is all on a mac, btw. How do I execute manually as www?
No, the 'www' doesn't belong to the sudo group most probably. Since it is automatically created upon installation of your web server, you don't have the password. Try giving 'www' (and/or the group 'www' is in) permission to execute the app in question.
cytryn
Forum Newbie
Posts: 6
Joined: Sat Jul 18, 2009 1:29 am

Re: exec failing to run imagemagick convert command

Post by cytryn »

The permissions are set to rwxr-xr-x already so I don't think that's the problem.

But I had the script spit out $PATH for www and discovered that /usr/local/bin (where convert is located) is not in it:
/bin:/sbin:/usr/bin:/usr/sbin:/usr/libexec:/System/Library/CoreServices

My /etc/profile has a conditional on adding this directory:

Code: Select all

# System-wide .profile for sh(1)
 
PATH="/bin:/sbin:/usr/bin:/usr/sbin"
export PATH
 
if [ "${BASH-no}" != "no" ]; then
        [ -r /etc/bashrc ] && . /etc/bashrc
fi
## setloginpath added /usr/local/bin start at Tue Feb  3 19:58:21 PST 2009
## Do not remove the previous line
if [ `whoami` != "root" ]
then
  PATH="$PATH:/usr/local/bin"
  export PATH
fi
## Do not remove the next line
## setloginpath added /usr/local/bin end at Tue Feb  3 19:58:21 PST 2009
## TeX added /usr/texbin start at Tue Feb  3 19:58:21 PST 2009
## Do not remove the previous line
if [ `whoami` != "root" ]
then
  PATH="$PATH:/usr/texbin"
  export PATH
fi
## Do not remove the next line
## TeX added /usr/texbin end at Tue Feb  3 19:58:21 PST 2009
 
That wouldn't prevent www from getting /usr/local/bin added to its path, would it? Any idea where I should look to figure out what's adding the /usr/libexec:/System/Library/CoreServices to the path? Having trouble tracking that down.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: exec failing to run imagemagick convert command

Post by prometheuzz »

cytryn wrote:...
That wouldn't prevent www from getting /usr/local/bin added to its path, would it? Any idea where I should look to figure out what's adding the /usr/libexec:/System/Library/CoreServices to the path? Having trouble tracking that down.
I don't think PATH is an issue here since you're not calling `convert ...` but you're using the absolute path `/usr/local/bin/convert ...`, so it doesn't matter what's in PATH (or what isn't). I'd look more closely in your log file(s) to see exactly why the user 'www' can't execute 'convert'.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: exec failing to run imagemagick convert command

Post by prometheuzz »

One more idea: perhaps `convert...` tries to write to some temporary directory/file where the user 'www' does not have permission to write to.
cytryn
Forum Newbie
Posts: 6
Joined: Sat Jul 18, 2009 1:29 am

Re: exec failing to run imagemagick convert command

Post by cytryn »

prometheuzz wrote:I don't think PATH is an issue here since you're not calling `convert ...` but you're using the absolute path `/usr/local/bin/convert ...`, so it doesn't matter what's in PATH (or what isn't).
Right, of course.
prometheuzz wrote:I'd look more closely in your log file(s) to see exactly why the user 'www' can't execute 'convert'.
I checked the httpd error_log file and discovered that gs is getting called but not getting found. Apparently, imagemagick's convert is calling ghostscript. So I tried to replicate the error in a terminal, not as user www. When I tried to run convert on a .ps file having /usr/local/bin in the path, it worked fine. I then removed /usr/local/bin from the path and tried running the convert again. This time I got the same error as I found in the log file:
sh: line 1: gs: command not found
I'm now trying to figure out how to set the path for www again (not for same reasons as above but this time it seems to make sense). I tried putting a .profile file in ~www (setenv PATH $PATH:/usr/local/bin) and a .bash_profile as well (export PATH=$PATH:/usr/local/bin) but neither worked. I'm not that familiar with shells though so maybe I'm doing this wrong?
cytryn
Forum Newbie
Posts: 6
Joined: Sat Jul 18, 2009 1:29 am

Re: exec failing to run imagemagick convert command

Post by cytryn »

prometheuzz wrote:One more idea: perhaps `convert...` tries to write to some temporary directory/file where the user 'www' does not have permission to write to.
Oh, I checked this too but the tmp directory I'm writing to is rwxrwxrwx and is accepting the .tex and ,dvi files so this seems to be ok.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: exec failing to run imagemagick convert command

Post by prometheuzz »

cytryn wrote:...
I'm now trying to figure out how to set the path for www again (not for same reasons as above but this time it seems to make sense). I tried putting a .profile file in ~www (setenv PATH $PATH:/usr/local/bin) and a .bash_profile as well (export PATH=$PATH:/usr/local/bin) but neither worked. I'm not that familiar with shells though so maybe I'm doing this wrong?
That looks like it should do the trick, but there could be some shell script that "resets" the PATH for user 'www', I guess.
You could also add a symbolic link to '/usr/local/bin/gs' (you're sure it's there?) in a directory that IS present in the system wide PATH so that the user 'www' can execute it.
cytryn
Forum Newbie
Posts: 6
Joined: Sat Jul 18, 2009 1:29 am

Re: exec failing to run imagemagick convert command

Post by cytryn »

prometheuzz wrote: That looks like it should do the trick, but there could be some shell script that "resets" the PATH for user 'www', I guess.
You could also add a symbolic link to '/usr/local/bin/gs' (you're sure it's there?) in a directory that IS present in the system wide PATH so that the user 'www' can execute it.
I gave up on fighting with the PATH - some mysterious things going on there. Your suggestion of a symbolic link worked perfectly though. Its working now. Thanks!
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: exec failing to run imagemagick convert command

Post by prometheuzz »

cytryn wrote:...
I gave up on fighting with the PATH - some mysterious things going on there. Your suggestion of a symbolic link worked perfectly though. Its working now. Thanks!
Good to hear that!
Post Reply