1

我想使用 PHP 执行一个 linux 命令

我的文件.php:

<?php
$output = shell_exec('ls');
echo "<pre>$output</pre>";
?>

有用!

但是当我将 linux 命令从lsto更改时reboot,什么也没有发生!

所以我试图找到另一种解决方案:

我的代码.html:

<button type="button" onclick="/var/www/myscript.sh">Click Me!</button>

myscript.sh:

sudo reboot

这也行不通!

谁能帮我解决这个问题。

谢谢你的帮助。

4

3 回答 3

6

By default, reboot command must be executed as root. You would be able to do that if your web server ran under root account, but this is very unusual proposition.

Typically, web server runs under limited account which cannot do much, and certainly cannot execute reboot. If you really want to do that, it must be done with great care. Standard way to provide this is to create special wrapper (most likely suid) which checks for many conditions before allowing to run under elevated permissions.

Another solution is to have PHP create flag file or insert special database entry, which would be checked by another service running as root, noticing that flag and finally executing reboot.

于 2013-10-01T06:35:16.603 回答
5

正如@mvp 所说,您不能以非 root 用户身份执行重启。

一个简单的方法是使用 cron 作业。

您的 myscript.sh 将是:

#!/bin/bash
touch /tmp/reboot.now

然后创建一个检查此文件是否存在的 cron 作业:

#!/bin/bash
if [ -f /tmp/reboot.now ]; then
  rm -f /tmp/reboot.now
  /sbin/shutdown -r now 
fi

然后将您的服务器配置为每分钟执行一次此脚本

* * * * * /usr/local/sbin/reboot.sh

当然,别忘了给文件赋予执行权限。

希望能帮助到你

已编辑:当然,您myscript.sh可以使用 phpfopenfclose代替

于 2013-10-01T07:01:40.280 回答
2

除了 Sal00m 的回答

crontab

* * * * * /usr/local/sbin/checkreboot.sh

检查重启.sh

#!/bin/bash
if [ -f /var/www/html/reboot.server ]; then
  rm -f /var/www/html/reboot.server
  /sbin/shutdown -r now 
fi

重启.php

<?php
$filehandler = fopen("/var/www/html/reboot.server",'w');
fwrite($filehandler,"Reboot now\n");
fclose($filehandler);
?>

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

于 2014-07-13T15:11:04.650 回答