You can not simply run command from php file to Reboot/Shutdown Raspberry Pi as you would from shell and it wouldn’t be very convenient to login to shell and issue Reboot/Shutdown command each time you need to bring reboot or shutdown your Raspberry Pi.
- You need to write script that would reboot Raspberry Pi.
- Some OS configuration changes to allow the reboot command from web application.
- Php page to execute script to shutdown or reboot Raspberry Pi.
Python Script for Reboot Raspberry Pi
Create a python script (i named it reboot.py) See Python Ref for sub-process command.
1 2 3 4 5 |
command = "/usr/bin/sudo /sbin/reboot" import subprocess process = subprocess.Popen(command.split(), stdout=subprocess.PIPE) output = process.communicate()[0] print output |
Python Script for Shutdown Raspberry Pi
Create a python script (i named it shutdown.py) See Python Ref for sub-process command.
1 2 3 4 5 |
command = "/usr/bin/sudo /sbin/shutdown -h now" import subprocess process = subprocess.Popen(command.split(), stdout=subprocess.PIPE) output = process.communicate()[0] print output |
OS Configuration Changes
For python code to work fully from web page it must run in the context of a user that has sufficient privileges. Run following command and go to the end of the file and add the following line
1 2 3 4 5 6 7 |
sudo visudo pi ALL=(ALL) NOPASSWD: ALL www-data ALL=/sbin/reboot www-data ALL=NOPASSWD: /sbin/reboot www-data ALL=/sbin/shutdown www-data ALL=NOPASSWD: /sbin/shutdown |
Above changes to enable user www-data to use the reboot and shutdown commands, press Ctrl+x to save and Ctro+x to exit.
PHP Side
For PHP – exec() vs system() vs passthru() please refere to this link for more details.
1 |
exec("python /var/www/reboot.py" |
One comment