tags:

views:

122

answers:

5

Is it possible to restart the pc using php?

+1  A: 

You can use exec() or system()-like method to run a system command to restart your computer.

Desintegr
A: 

Typically you need superuser rights to restart a computer. But it is a bad practice to run web server as superuser. I'd recommend to do some interprocess communication. E. g. PHP script writes a secret key in a file and then a cron job reads the file and restarts the computer.

And I'm not sure that it is a good idea to restart a computer via Web at all. Why would you want to do it?

codeholic
Some server administration tools like Plesk allow it.
zneak
+2  A: 

It is true that you can use exec() or system() but the permission will be the problem as PHP is run on the user that run apache.

As a quick workaround, the user apache may be given permission to restart. Depending on your system, setting this can be quite hard.

Another way you can do is to have a Cron task (that belong to root) checking some file every minutes (or seconds) and have that file edited by your PHP program. The file is set to be editable by only Apache. This is much easier to do.

Hope this helps.

NawaMan
A: 

This answer pertains to a linux-based server.

DANGEROUS: From php use system(), in system, use "sudo shutdown -r now " to restart and "sudo shutdown -h now" for shutdown.

In /etc/sudoers, add permission to run shutdown for the apache user

One problem is that sudo can prompt for a password.

Either remove apache user's password (but then don't run sshd, telnetd, ftpd, etc...) or see if your /etc/sudoers has a passwordless configuration. "man sudoers" about line 200 and again about line 270 says Tag_Spec can include NOPASSWD to specify that the user does not have to re-authenticate with their password to run a sudo command.

SAFER: token/cron Have php write a file somewhere. Write a script run by cron as root so that if this file exists, the file is removed and then shutdown or restart is run.

Paul
+1  A: 

In our system, we do the one told by Paul.

  1. Have a script that will write to some command dir (eg: /data/command/). You have to hardened this script though.
  2. Have a cron that run per-minute that will check your command dir. If it has a restart command, that cron will fire a restart command to system.
silent