I want to run a python script on Raspberry Pi via web-app. I have a PHP page for a button, and when the button is clicked it goes to another PHP page which is made up of the following code. If I type system("mkdir folder")
it does work but it does not work for a python script. I have already changed the sudeors
file using www-data ALL=NOPASSWD: ALL
.
<?php
system("sudo python3 x.py");
?>
#!/usr/bin/env python3
import os
os.system("gnome-terminal")
Try with
<?php echo shell_exec("python x.py");?>
This command should echo
out the result of the executed command, useful for debugging since it will show errors that you might have either in the shell command or in the python script.
or
<?php echo exec("python x.py");?>
Also make sure and verify that your php
engine has enough permissions to run shell commands. You could also try to disable safe-mode but it is deprecated and not recommended by any means.
Also from the comments in the references:
If you are running php as an Apache module in Unix then every system command you run is run as user apache. This just makes sense.. Unix won't allow privileges to be elevated in this manner. If you need to run a system command with elevated privileges think through the problem carefully!
As a side note, if you are trying to have an interactive webpage where you input stuff in it and it acts as a remote terminal shell that is defenitely not how to do it, it is a bit more complicated since it would require bidirectional connection in """" real time """". You would have to use some AJAX
or a WebSocket
connection with a WebSocket
server on your raspi.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.