ssh2_exec rand returns process 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 ))) {
r开发者_StackOverflow社区eturn "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;
}
}
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.
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.
EDIT:
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.
精彩评论