如何使用 php 重新启动 Linux 系统 (Debian)


How to reboot linux system (Debian) by using php

我想使用 PHP 执行 linux 命令

我的文件.php:

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

它有效!

但是当我将 linux 命令从 ls 更改为 reboot 时,没有任何反应!

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

mycode.html:

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

myscript.sh:

sudo reboot

这也行不通!

愿有人帮助我解决这个问题。

感谢您的帮助。

默认情况下,重新启动命令必须以 root 身份执行。如果您的 Web 服务器在 root 帐户下运行,您将能够这样做,但这是非常不寻常的提议。

通常,Web服务器在有限的帐户下运行,该帐户不能做太多事情,当然也无法执行重新启动。如果你真的想这样做,就必须非常小心地完成。提供此功能的标准方法是创建特殊的包装器(很可能是 suid),该包装器在允许在提升的权限下运行之前检查许多条件。

另一种解决方案是让 PHP 创建标志文件或插入特殊的数据库条目,这将由另一个以 root 身份运行的服务检查,注意到该标志并最终执行重新启动。

正如@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可以是带有fopenfclose的php

除了Sal00m的答案

crontab

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

checkreboot.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