SSH not seeming to run in bash script called via web server

bashPHPscpssh

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?

Best Answer

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.