Its just for development purposes so at the end of the day I like to shut it down that usually means logging into ssh and typing "sudo reboot" or "sudo poweroff" followed by a prompt for my root password (i'm using ubuntu server, which incidently i highly recommend over fedora). Being the person that I am I thought "hey wouldn't it be cool to do this via my intranet with PHP" so I've been looking about for how to do this and trying stuff myself and so far I haven't been able to do it.
Here are some of my attempts:
Code: Select all
try {
// i'm using the backtick operator here http://www.php.net/manual/en/language.o ... cution.php
if(!$out = `sudo reboot\nrootpass`) throw new Exception('didn\'t work');
echo $out;
}
catch(Exception $e) { errHandle($e); }Code: Select all
try {
if(!$out = `./scriptThatRebootsWhenRunFromSSH.sh`) throw new Exception('didn\'t work');
echo $out;
}
catch(Exception $e) { errHandle($e); }Code: Select all
try {
$fh = fopen('php://stdin','w');
if(!is_resource($fh)) throw new Exception('couldn\'t open stdin');
try {
if(!fputs($fh,"sudo reboot\nrootpass")) throw new Exception('couldn\'t write to stdin');
fclose($fh);
}
catch(Exception $e) { errHandle($e); }
}
catch(Exception $e) { errHandle($e); }I stubbled across this solution but I don't really like the idea of something being cronned every minute:
A more easy solution is that:
Make a cron that executes every minut /tmp/rreboot.sh for user root
crontab -e
Add the line:
* * * * * /tmp/rreboot.sh
The script is:
/tmp/rreboot.sh
---------------
#!/bin/sh
if [ -f "/tmp/rreboot" ]; then
rm -f /tmp/rreboot
shutdown -r now
fi
---------------
Make executable the file with chmod +x /tmp/rreboot.sh
And a simple PHP like:
<?php
exec("echo rreboot > /tmp/rreboot");
echo "Ok, i'll reboot in a few seconds";
?>
So, when anyone calls to this PHP, creates a file, that if its detected by rreboot.sh, the root will reboot the machine.