简体   繁体   English

如何在php中执行脚本

[英]How to execute a script in a php

I have an overhead controller (sort of a magic wand for some robots) plugged into my usb port. 我的USB端口中插入了一个头顶控制器(有些机器人是魔杖)。 I am trying to create a web server on linux that allows users to send a preloaded code (so no one is inputting anything) to the robots. 我正在尝试在Linux上创建一个Web服务器,该服务器允许用户向机器人发送预加载的代码(因此没有人输入任何东西)。 I have embedded the command in a script file that I am trying to execute in a php file. 我已将该命令嵌入脚本文件中,而该脚本文件试图在php文件中执行。 The bit of code that executes the script file in php is run when a button is pressed on the web server. 当在Web服务器上按下按钮时,将运行执行php中脚本文件的代码。 However, when I press the button nothing happens. 但是,当我按下按钮时,什么也没有发生。 The code doesn't get sent to the controller and no errors are given. 该代码不会发送到控制器,并且不会给出任何错误。 I can run the script in the command line with no problems. 我可以在命令行中毫无问题地运行脚本。 But when I try to execute it run it from the web server, nothing happens. 但是,当我尝试执行它并从Web服务器运行它时,什么也没发生。 Is there a way I can resolve this? 有办法解决吗? This is what's in the php file 这是PHP文件中的内容

<html>
<body>
<h1><font face="verdana" color="green">SwarmArena</font></h1>
<hr>
<form action = "<?php $_PHP_SELF ?>" method = "POST">
Name: <input type ="text" name = "name"/>. ; <br>
Age: <input type ="text" name="age"/>. ; <br>

New Code: <textarea cols=40 rows=10 name="Newcode">Default text</textar$
<input type = "submit" name="enter"  value="Enter" />
<input type = "submit" name="insert" value="insert" />
<input type = "submit" name="select" value="select" />
<input type = "submit" name="sleep"  value="sleep" />
<input type = "submit" name="reset" value="reset" />
</form>
</body>
</html>

<?php

if(isset($_POST['sleep'])){
$out = shell_exec('./sleep.sh');
echo "$out";
}
?>

this is what's in the sleep.sh 这就是sleep.sh中的内容

#!/bin/sh

 cd kilocmd && ./kcmd -s /dev/ttyUSB0 -c sleep 
 echo "done";

Thanks 谢谢

I don't know how you are doing this. 我不知道你是怎么做到的。 But onclick the button it should submit to a php page which will run the shell scripts using exec and shell_exec php commands . 但是onclick按钮应该提交到一个php页面,该页面将使用exec and shell_exec php命令运行shell脚本。

More Over that Several Possibilities May Cause Issues like 更重要的是,几种可能性可能会引起诸如

 - exec and shell_exec are disabled in php.ini
 - The path to the executable is wrong. If the script is in the same
   directory as the php file, try exec(dirname(__FILE__) .
   '/myscript.sh');

Assuming you are executing the command correctly from the PHP script, it may be useful to look into permission issues. 假设您从PHP脚本正确执行了该命令,那么研究权限问题可能会很有用。

Log into the user that the web-server is running under and try executing the command manually from there. 登录到正在运行Web服务器的用户,然后尝试从那里手动执行命令。 I personally have my webserver running under a user named apache . 我个人的网络服务器在名为apache的用户下运行。

To view your current user run whoami in the command line. 要查看当前用户,请在命令行中运行whoami

To change user run su - newuserhere . 要更改用户,请在su - newuserhere运行su - newuserhere

If this works, or you are running the web-server under root then you can most likely rule-out any permission issues. 如果这行得通,或者您是在root用户下运行Web服务器,则很可能可以排除所有权限问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM