使用PHP启动脚本时,脚本中的执行屏幕会失败


Executing screen within a script fails when script is launched with PHP

我使用php在我的服务器上执行脚本。
PHP: exec("screen -dmS testingscreen /home/username/otherfolder/test.sh")
有一段时间我无法做到这一点,直到我做了一些挖掘,发现了一些关于如何做到这一点的事情。我还发现了一些与我同时遇到的屏幕/小问题有关的其他事情。

我将详细展示每个问题,直到最后一个问题,我似乎无法找到解决办法。


问题一(已解决):试图在webroot外运行脚本。
主题:使用PHP在web根目录外执行脚本。
解决方案:使用sudo chown www-data:www-data filename.sh让我的web用户访问脚本。


问题二(已解决):php文件调用时,脚本不执行任何操作。
解决方案:使用chmod +x filename.sh
使脚本可执行注意:当我在脚本目录中使用ls时,很明显我做错了什么。


问题三(已解决):无法su到www-data检查屏幕是否正在运行。
错误:此帐户目前不可用。
问题:www-data在/etc/passwd
中设置为/bin/nologin解决方案:chsh -s /bin/bash www-data,这样我就可以切换到它作为用户。


问题四(已解决):在su www-data中无法使用screen -r testingscreen
错误:无法打开终端'/dev/pts/0' -请检查。
问题:权限,登录帐户下没有打开屏幕,并试图使用su帐户。
解决方法:在终端使用screen -r testingscreen之前先使用script /dev/null


问题五(未解决):
执行screen -dmS testingscreen /home/username/anotherfolder/filename.sh
时与php的exec()做屏幕testingscreen

脚本文件中有一行写着
screen -dmS scriptscreen mono '/home/username/otherfolder/myprogram.exe' 'programargs1' 'programargs2' 'args3' 'args4'
当使用脚本"filename.sh"执行时,此屏幕无法运行
但是,作为www-data用户执行这同一行(使用sudo su www-data作为该用户)可以正常工作。

同样值得注意的是:运行screen -dmS testingscreen /home/username/otherfolder/test.sh工作绝对很好,启动时应该包括scriptscreen。这可以在使用sudo su www-data时工作,但如果我用php启动脚本则不行。

更奇怪的是,在php启动的脚本文件中使用screen -dmS testingtop top工作得很好。我是不是漏掉了什么明显的东西?

我已经检查了文件权限,一切都很好,它应该工作。能够以www-data的方式运行我的程序意味着这些条件设置得很好。这似乎与php或我的服务器有关。任何帮助或可能的提示都是非常欢迎的,我希望我详细的问题和解决方案可以在未来的一篇文章中帮助其他人。

既然你是调用sudo作为你的问题的一部分,我认为这是一个尝试让PHP执行脚本与根权限?

我最近发布了一个项目,该项目允许PHP获取一个真正的Bash shell并与之交互。点击这里下载:https://github.com/merlinthemagic/MTS

下载后,您只需使用以下代码:

//if I misunderstood your question and the script does not require 
//root permissions you can change the
//second argument on getShell() to false. That will return a bash shell
//with permissions from www-data.
$shell    = 'MTS'Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1  = $shell->exeCmd('/home/username/otherfolder/test.sh');