[英]How to execute a terminal script in Linux from a php file
So I am sorry if I mess up the names a little. 所以,如果我把名字弄乱了,我很抱歉。 I am still pretty new to linux. 我对linux还是很陌生。
I setup a headless raspberry pi webserver. 我设置了一个无头的树莓派Web服务器。 I installed TightVNC Server on it so I could remote in but for the life of me I can't get the damn server to start when my pi reboots. 我在上面安装了TightVNC服务器,因此可以远程进入,但是对我来说,当我的pi重新启动时,我无法启动该死的服务器。
My work around to this is to have a php script that I can call from the web that executes a terminal script that starts TightVNC. 我对此的解决方法是要有一个php脚本,可以从网上调用它来执行启动TightVNC的终端脚本。 I don't know if it's called terminal script or not but in windows its called a bat file. 我不知道它是否称为终端脚本,但在Windows中称为蝙蝠文件。
Do php files have this permission? php文件是否具有此权限? If so what is the php command to execute this file? 如果是这样,执行该文件的php命令是什么?
To run a shell command with PHP, use shell_exec . 要使用PHP运行shell命令,请使用shell_exec 。 It will run as the web user (say, www for Apache), so that user will need to have appropriate permissions. 它将以Web用户身份运行(例如,对于Apache,为www),因此该用户将需要具有适当的权限。 You could, of course, use sudo to get around that - but that is extremely insecure and a very, very bad idea. 您当然可以使用sudo来解决此问题-但这是非常不安全的,而且是一个非常非常糟糕的主意。
So if you had, eg a shell script called shellscript.sh sitting on yoru server, and you've written it to do whatever you need it to do, you could run it in php with: 因此,如果您有一个名为yoshell服务器的shell脚本,例如shellscript.sh,并且已经编写了它可以执行所需的操作,则可以在php中使用以下命令运行它:
shell_exec('sh path/to/shellscript.sh')
However - I'm not sure I correctly understand what you're trying to do, but it seems to me this is a hacky, very inefficient way to go about things, and it's very likely there is a much smoother solution. 但是-我不确定我是否正确理解了您要做什么,但是在我看来,这是一种处理不便,效率很低的方法,很可能有一个更加流畅的解决方案。 Even so, good luck. 即使这样,祝你好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.