问题
This is the code i have so far
site:
<?php
if ($_POST['action']=="Shutdown") {
$test=shell_exec("touch /scripts/shutdown.sh");
}
?>
shutdown.sh:
#!/bin/sh
if [ -f "/tmp/shutdown" ]
then
rm -f /tmp/shutdown
/sbin/shutdown now
fi
I roughly got the idea from http://ubuntuforums.org/ But, when i run the php script on my local-host, it doesn't show up?
Question: How to shutdown my system via a php site using LAMP server?
Thanks in advance!
回答1:
No need to create a script for it, you can just do:
shell_exec('sudo /sbin/shutdown -h now');
回答2:
touch
only updates the file's modification time, it doesn't execute the script.
What you need to do is to give the user the PHP/web server process is running as the permission to execute the shutdown
command, a permission typically reserved for root and not ordinary web processes. The best way is probably through the sudoers file, giving the user (probably www-data
or so) the permission to execute sudo shutdown
via exec()
without password prompt. Read the man sudoers
configuration for your system. Be extremely careful not to give permissions beyond this or to otherwise screw up your sudoers configuration.
The sudoers file needs an entry like:
www-data ALL=NOPASSWD: /sbin/shutdown
Again, read up on man sudoers
to be sure what you're doing here. Only use visudo
to edit the sudoers file to make sure it doesn't mess up.
Then:
exec('sudo /scripts/shutdown.sh');
来源:https://stackoverflow.com/questions/17643094/how-to-shutdown-my-system-via-a-php-site-using-lamp-server