SSH似乎不能在通过web服务器调用的bash脚本中运行

我有一个简单的shell脚本:

  1. 在远程机器上创build一个目录。
  2. 将.epub文件复制到远程机器的新目录。
  3. 运行kindlegen将.epub转换为.mobi。
  4. 远程服务器将新的.mobi文件复制回原始服务器。

两台服务器都通过公钥configuration了SSH无密码login。

在命令行运行脚本运行良好。 从PHP shell_exec命令从服务器运行它将执行脚本,但ssh和scp命令似乎不会被执行,或者至less不输出任何内容,即使我使用-v标志。

这是调用shell脚本的PHP函数:

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); } 

以下是带有失败命令的shell脚本:[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 

如果我从脚本文件运行这一系列命令,或者从命令行执行单独的命令,那么这一切完美无缺。

当通过php脚本运行时,如果我决定回显testing命令,则可以看到脚本输出,如果我中断源文件名,scp将报告错误,但是如果scp或ssh命令不正确,则根本没有任何反应。 甚至没有从-v标志的详细debugging输出。

我必须失去明显的东西? 我猜这是因为PHP正在使用SSH不喜欢的用户“nobody”。 有没有办法解决这个问题呢?

你可能想尝试添加

  </dev/null 

在有问题的命令结束时,只是在命令需要input的情况下。

private ssh密钥存储在哪里,在你运行命令的用户的〜/ .ssh / id _…文件中?

如果是这样,那么apache不作为你的用户运行。 Apache需要访问ssh密钥的副本。 你也可以将-i添加到你的ssh和scp命令中,这样它就不必存在于apache的主目录中。

shell_exec ()函数可能会给你一些你没有收集到的信息。 尝试

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

这是我会做什么来开始debugging这个问题。

编辑:

将.ssh和id_rsa上的priv设置为777会导致你的主要问题,因为如果ssh过于宽容,ssh拒绝工作。 将它们分别重新设置为700和600。

我不记得你为什么没有看到任何回来的尝试

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

什么也应该强制stderr也std出来。

当我遇到麻烦从httpd和php进程执行外部脚本,我尝试手动运行在同一个用户作为httpd进程运行。

su nobody

nobody用户没有设置shell的可能性非常高,您需要启用一个shell来启动它。