stream_set_timeout 不适用于 PHP 中的 ssh2_exec [英] stream_set_timeout not working for ssh2_exec in PHP

查看:57
本文介绍了stream_set_timeout 不适用于 PHP 中的 ssh2_exec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 PHP 中尝试为我的 ssh2_exec 连接设置超时时遇到问题,以便在运行命令时出现问题时,它会释放正在执行的线程并且不会卡住整个网站.我已经尝试过 stream_set_timeout($stream,$seconds) 但它似乎没有按预期工作.

I am having trouble when try to set timeout for my ssh2_exec connection in PHP, so that when there is something wrong when running the command, it will release the executing thread and will not jam the whole web site. I have tried stream_set_timeout($stream,$seconds) but it seems not to work as expected.

对此有什么想法吗?

//run specified command (ssh)
    function runCMD($cmd){
        if (!($stream = ssh2_exec($this->conn, $cmd )))
        {
            echo "fail: unable to execute command\n";
            fclose($stream);
            return ERROR_SSH_EXEC_COMM;
        }

        sleep(1);
        stream_set_timeout($stream,5);//>>not work
        stream_set_blocking($stream, true);
        $res = stream_get_contents($stream);

        return $res;
    }

推荐答案

对于任何想知道如何设置超时的人,有一个 default_socket_timeout php.ini 中的变量,可用于设置当前会话中任何基于套接字的连接的超时时间.

For anyone wondering how to set the timeout, there's a default_socket_timeout variable in php.ini that can be used to set the timeout for any socket-based connections in your current session.

你可以通过简单地运行来检查它

You can check it by simply running

$timeStart = time();
ini_set("default_socket_timeout", 5);
$ssh = ssh2_connect('123.123.123.123');
echo (time() - $timeStart), PHP_EOL;

这篇关于stream_set_timeout 不适用于 PHP 中的 ssh2_exec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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