Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a simple shell script that:

  1. Makes a directory on the remote machine.
  2. Copies a .epub file to the remote machine's new directory.
  3. Runs kindlegen to convert the .epub to a .mobi.
  4. The remote server copies the new .mobi file back to the originating server.

Both servers are configured with SSH passwordless login via public key.

Running the script at the command line works well. Running it from the server from a PHP shell_exec command will execute the script, but the ssh and scp commands do not seem to get executed, or at least don't output anything, even if I use a -v flag.

Here's the PHP function that calls the shell script:

function mobi_watermark($userid, $email, $epubpath)
{

    $outpath = "/tmp/epub$userid/"; # the book gets marked for each user, store each epub under a unique work folder to avoid collision
    shell_exec("chmod -R 777 ".$outpath); # web server creates files under user "nobody".
    shell_exec("del.sh"); # deletes old files if they exist
    shell_exec("rep.sh $userid $email $epubpath"); # creates the initial .epub and tmp dir
    shell_exec("chmod -R 777 ".$outpath); 
    shell_exec("kindle.sh $outpath"); # this is the mobi conversion, where the problem is
    $this->mobi_ufa_download('ufa-187.mobi',$outpath);
}

And here's the shell script with the failing commands: [ kindle.sh ]

#!/usr/local/bin/bash
# uses the same /tmp$userid path as the other scripts, but on the remote server
TMPPATH=$1 
cd $TMPPATH
# create remote dir
ssh -v user@server 'mkdir -v '$TMPPATH
# copy the source epub file to remote
scp -v $TMPPATHfile-name.epub user@server:$TMPPATH
# perform the conversion and copy the .mobi result back to originating server
/usr/bin/ssh -v user@server 'cd '$TMPPATH'; /home/username/kindlegen/kindlegen ./file-name.epub; scp -v file-name.mobi user@originating-server:'$TMPPATH';rm -rf '$TMPPATH 

If I run this series of commands either from a script file or as individual commands from the command line myself, it all works perfectly.

When run via the php script, I can see script output if I decide to echo test commands, and scp will report an error if I break the source filename, but simply nothing happens for scp or ssh commands if they are otherwise correct. There's not even any verbose debug output from the -v flag.

I must be missing something obvious? I'm guessing it's because PHP is using user 'nobody' which SSH does not like. Is there a way around that, if that is the issue?

share|improve this question
add comment

4 Answers

You might want to try adding

 </dev/null

at the end of the offending commands just in the event that somehow the commands needed input.

share|improve this answer
add comment

Where is the private ssh key stored, in the ~/.ssh/id_... file of the user you run the command as?

If so, then apache isn't running as your user. Apache needs to have access to a copy of the ssh key. You can also add the -i to your ssh and scp commands so it doesn't have to exist in apache's home dir.

share|improve this answer
    
I tried adding -i /path/to/user/home/.ssh/id_rsa but this had no effect. Also tried chmod'ing .ssh and id_rsa to 777, still no effect. –  Ian Jan 13 '12 at 20:19
    
As Iain said, ssh likes safe permissions. Try copying the file to somewhere apache already has access, and change the owner to the user that apache runs as. –  becomingwisest Jan 13 '12 at 21:38
add comment

The shell_exec() function is likely giving you some information which you are ignoring by not collecting it. Try

$info=shell_exec("kindle.sh $outpath");
echo "<pre>$output</pre>";

it's what I would do to start debugging this problem.

Edit:

Setting the privs on .ssh and id_rsa to 777 is going to cause you major problems as ssh refuses to work if they are too permissive. Set them back to 700 and 600 respectively.

I don't undersand why you're not seeing anything come back try

$info=shell_exec("kindle.sh 2>&1 $outpath");
echo "<pre>$output</pre>";

WHich should force stderr to std out too.

share|improve this answer
    
My debugging code actually included this, but it's important to note! Thank you. –  Ian Jan 13 '12 at 19:57
    
@Ian: see my update. –  Iain Jan 13 '12 at 21:23
    
I do see output from kindle.sh in general, just not from the scp or ssh commands inside of kindle.sh in cases where they would otherwise work. if i use a non-existent local source file, scp will complain and i will see that in the output. but if everything should work, ssh and scp are silent. –  Ian Jan 13 '12 at 22:04
    
@Ian: Do your apache error logs have anything interesting in them ? –  Iain Jan 13 '12 at 22:13
    
@Ian: try scp -v $TMPPATHfile-name.epub user@server:$TMPPATH 2>&1 too. –  Iain Jan 13 '12 at 22:20
add comment

When I run into trouble executing external scripts from httpd and php processes, I try manually running under the same user as the httpd process runs as.

su nobody

It's a very high probability that the nobody user does not have a shell set, you will need to enable a shell to su to it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.