简体   繁体   中英

How to shutdown my system via a php site using LAMP server?

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!

无需为其创建脚本,您可以执行以下操作:

shell_exec('sudo /sbin/shutdown -h now');

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

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.

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