ssh2_exec rand 返回进程 ID? [英] ssh2_exec rand returns process id?

查看:22
本文介绍了ssh2_exec rand 返回进程 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时当我运行此代码时,我会得到一个进程和 ID,有时命令会执行但数据返回保持为空?有人知道如何正确捕捉流吗?

Sometimes when i run this code I get a process and id and sometimes the command will execute but the data return stays empty? Does anybody know how to catch the stream properly?

/**
 * Run command in background and returns the process id
 * 
 * @param string $cmd
 * @return int process id
 */
public function runBackground($cmd)
{
    $cmd = $cmd." > /dev/null & echo $!";

    if (!($stream = ssh2_exec($this->_connection, $cmd ))) {
        return "fail: unable to execute command\n";
    } else {
        // collect returning data from command
        stream_set_blocking($stream, true);
        $data = "";

        while ($buf = fread($stream,4096)) {
            $data .= $buf;
        }

        fclose($stream);
        return (int)$data;
    }       
}

推荐答案

我上次使用 ssh2 PECL 扩展时遇到了类似的问题.它似乎与 libssh2 以某种方式有关:我之前没有遇到任何问题,然后我升级了 libssh2 并开始无缘无故地偶尔收到空响应.ssh2_exec 仍然会执行命令,但有时我会得到输出,有时我不会.

I had similar problems with my last use of the ssh2 PECL extension. It seems to be related to libssh2 somehow: I had no problems with it before, then I upgraded libssh2 and started receiving empty responses sporadicly for no apparent reason. ssh2_exec would still execute the command, but sometimes I would get output, sometimes I wouldn't.

不幸的是,此扩展程序自 2008 年以来就无人维护,并且仍处于 Beta 状态 (0.11.0).您可能想查看一些 SSH2 3rd 方库(其中一些是 LGPL 或 GPL).我一直无法解决这个问题.

Unfortunately, this extension is unmaintained since 2008 and is still in Beta state (0.11.0). You may want to look at some SSH2 3rd party libraries (some of them are LGPL or GPL). I've never been able to fix that issue.

2 天前(2010 年 11 月 3 日),维护者发布了一个补丁,该补丁显然适用于最新的 libssh2,尽管我自己还没有测试过.您可能想要升级到 0.11.2.

2 days ago (on November 3rd, 2010), the maintainer released a patch that apparently works with the latest libssh2, although I haven't tested it myself. You may want to upgrade to 0.11.2.

这篇关于ssh2_exec rand 返回进程 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆