如何通过php系统运行'at'命令()

时间:2012-05-22 19:43:52

标签: php unix

出于某种奇怪的原因,这个

echo system("echo 'echo hello > /dev/pts/2' | /usr/bin/at 19:36");

拒绝使用我的php脚本工作,但是当我通过命令行输入命令时,命令工作正常。

我知道php有权执行一些命令。我可以从php脚本运行'ls'而不是'at'命令。我试过玩文件权限,但到目前为止无济于事(

修改

/ usr / bin / at的权限是:

-rwxr-sr-x 1 daemon daemon 42752 Jan 15 2011 at

我认为这是一个权限问题,如果我从我的ssh终端执行php文件它运行正常,但不是来自网络。

2 个答案:

答案 0 :(得分:2)

您正在执行的是

echo 'hello' > /dev/pts/2 | /usr/bin/at 19:36

含义

echo 'hello' > /dev/pts/2

并将stdout传递给/usr/bin/at 19:36,但由于您已将回显重定向到/dev/pts/2,因此它将为空。你可能想要做的是:

echo system("echo 'echo hello > /dev/pts/2' | /usr/bin/at 19:36");

您可能还希望使用shell_exec通过shell或proc_open传递命令,这样可以更好地控制正在执行的命令的stdin / out / err。您的示例将对应于(改编自php.net docs的示例):

<?php
$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
   2 => array("pipe", "w")  // stderr is a pipe that the child will write to
);

$process = proc_open('/usr/bin/at', $descriptorspec, $pipes);

if (is_resource($process)) {
    fwrite($pipes[0], 'echo "hello" > /dev/pts/2');
    fclose($pipes[0]);

    $stdout = stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    $stderr = stream_get_contents($pipes[2]);
    fclose($pipes[2]);

    $return_value = proc_close($process);
    echo "command returned $return_value. stdout: $stdout, stderr: $stderr\n";
} else {
    echo "Process failed";
}
?>

答案 1 :(得分:0)

在你的php.ini文件中检查disable_functions,出于安全原因,有时会禁用像system这样的函数。

相关问题