我想创建一个 php 脚本来执行 shell 命令并返回其输出。服务器需要私钥。当我第一次决定测试它时,我创建了这个:
<?php
$command = "ls";
$output = shell_exec($command);
echo "<pre>$output</pre>";
?>
那工作得很好。但是当我更改$command
为我真的想运行的命令时:
$command = "/etc/init.d/mycontrollerd status /etc/mycontrollerconfig";
它给了我这个输出:
You need root privileges to run this script
我的猜测是我需要使用sudo
. 当然,这需要将 pem 文件放在服务器上的某个位置。假设我这样做,究竟应该$command
是什么?我应该使用shell_exec()
,exec()
还是system()
其他什么?