tags:

views:

69

answers:

5

i have try some command like system(reboot) exec(reboot) and system("/ect/init.d/networking restart"); but not happen something

how can i do?

A: 

Try this:

<?php
shell_exec("/usr/sbin/reboot");
exec("/usr/sbin/reboot");
system("/usr/sbin/reboot");
?>

For more details look here:

http://www.linuxquestions.org/questions/linux-newbie-8/shutdown-and-reboot-linux-system-via-php-script-713379/

infinity
Isn't reboot normally in /sbin?
awoodland
@awoodland you're right, changed
infinity
On my test system at least it's /sbin/reboot, not /usr/sbin/reboot. FHS (http://www.pathname.com/fhs/pub/fhs-2.3.html#SBINSYSTEMBINARIES) implies that this is the right place too.
awoodland
+4  A: 

If you're trying to do this through apache and the CGI version of PHP -- then the answers is "no". PHP will execute as the same user as apache and giving this user access to your system would be extremely dangerous.

bogeymin
A: 

Probably your PHP interpreter isn't running as a user with suitable permissions to do this. Normally these need to be done as root, which is a bad idea for PHP in general since one small security hole could see the whole machine being compromised.

If you really want to do this I'd suggest looking at using sudo to grant limited extra access for the user the webserver runs as.

The other alternative to sudo on some systems is dbus. With correct dbus privileges you can send an instruction to restart e.g.

dbus-send --system --print-reply --dest="org.freedesktop.Hal" /org/freedesktop/Hal/devices/computer org.freedesktop.Hal.Device.SystemPowerManagement.Shutdown

Works from the command line and I believe there are dbus bindings available for php.

awoodland
+2  A: 

as everyboy stated, it is quite dangerous. Better use some ssh session to reboot/restart your server/services.

In the case you still want to do it give your apache running user (www) a sudo right.

dvhh
+2  A: 

You'll have to add your apache user to sudoers with NOPASSWD and only give access to the i.e. reboot and then run system("sudo reboot");

For the sudoersfile:

www-data reboot = NOPASSWD: /sbin/reboot

This will give apache access to reboot your server, but rememer that all users on the system then will be able to reboot.

Ole Melhus