开发者

restart pc using php

Is it possible t开发者_StackOverflow社区o restart the pc using php?


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.


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


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.


I had the same need for a CarPC setup running Debian/Lenny. I configured inetd to run /sbin/reboot whenever a connection was made to the port for that service.

I then had a PHP script do an fsockopen() to the localhost/port and it performs the reboot instantly.

Quick setup (debian - modify for your system):

In /etc/services add a line "reboot 88/tcp" (or whatever port you want) In /etc/inetd.conf add a line "reboot stream tcp nowait root /sbin/reboot" Restart inetd using /etc/init.d/openbsd-inetd restart

Now connect to localhost:88 and it will reboot the system.

If your system runs xinetd you can configure it to accept only connections from localhost to tighten the security a bit. In any case, this is still a hack.


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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜