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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – SSH似乎不能在通过Web服务器调用的bash脚本中运行脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个简单shell脚本:

>在远程计算机上创建目录.
>将.epub文件复制到远程计算机的新目录.
>运行kindlegen将.epub转换为.mobi.
>远程服务器将新的.mobi文件复制回原始服务器.

两台服务器都通过公钥配置SSH无密码登录.

在命令行运行脚本运行良好.从PHP shell_exec命令从服务器运行它将执行脚本,但ssh和scp命令似乎没有执行,或者至少不输出任何内容,即使我使用-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脚本运行时,如果我决定回显测试命令,我可以看到脚本输出,如果我中断文件名,scp将报告错误,但是如果scp或ssh命令正确则没有任何反应. -v标志甚至没有任何详细的调试输出.

我一定错过了一些明显的东西?我猜这是因为PHP正在使用SSH不喜欢的用户’nobody’.如果这是问题,有没有办法解决这个问题?

您可能想尝试添加
</dev/null

在违规命令的最后,只是在某种程度上命令需要输入的情况下.

脚本宝典总结

以上是脚本宝典为你收集整理的php – SSH似乎不能在通过Web服务器调用的bash脚本中运行全部内容,希望文章能够帮你解决php – SSH似乎不能在通过Web服务器调用的bash脚本中运行所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。