Tobi Ade-Odiachi
Tobi Ade-Odiachi

Reputation: 11

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. 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. I have embedded the command in a script file that I am trying to execute in a php file. The bit of code that executes the script file in php is run when a button is pressed on the web server. 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. Is there a way I can resolve this? This is what's in the php file

<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

#!/bin/sh

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

Thanks

Upvotes: 0

Views: 141

Answers (2)

Achyut Krishna Deka
Achyut Krishna Deka

Reputation: 759

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 .

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

Upvotes: 3

Tom Wright
Tom Wright

Reputation: 2861

Assuming you are executing the command correctly from the PHP script, it may be useful to look into permission issues.

Log into the user that the web-server is running under and try executing the command manually from there. I personally have my webserver running under a user named apache.

To view your current user run whoami in the command line.

To change user run su - newuserhere.

If this works, or you are running the web-server under root then you can most likely rule-out any permission issues.

Upvotes: 0

Related Questions