I have a simple shell script that:
- Makes a directory on the remote machine.
- Copies a .epub file to the remote machine's new directory.
- Runs kindlegen to convert the .epub to a .mobi.
- 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?
You might want to try adding
at the end of the offending commands just in the event that somehow the commands needed input.
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.
The shell_exec() function is likely giving you some information which you are ignoring by not collecting it. Try
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
WHich should force stderr to std out too.
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.