douzhi1879
2018-10-19 22:26
浏览 84
已采纳

PHP长时间运行脚本超时

I am creating a page that executes a shell script on a remote server to scan a website and outputs the results on the screen. The output can sometimes take awhile to get depending on the size of the site being scanned. Currently the script works and does what it's supposed to but the problem is when I scan larger sites it stalls and on the platform the website is being hosted on has a timeout of 30 seconds that I cannot alter.

I am wondering what the best way to keep the connection alive whether it just be sending dots to the screen or maybe something else just to keep the connection alive.

Here is my script

$ssh = new Net_SSH2('hostname');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

$ansi = new File_ANSI();
$ssh->enablePTY();
$ssh->setTimeout(60);
$ssh->exec("./test.sh | awk 'NR >= 16 {print}'
");
$ansi->appendString($ssh->read());
echo $ansi->getHistory();

Any help or guidance is deeply appreciated.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douzi2749 2018-10-19 22:43
    已采纳

    You should rather let the page load and e.g. run an AJAX request that will wait for a reply/listen on a port than trying to keep the connection alive.

    So on the user's side, it would run an ajax request (javascript) to the php url, then on success you display the result.

    $.ajax({
      url: "/thescript.php":,
      type: "POST",
      datatype: "POST"
      success: function(){
        //do display stuff
      }
    
    });
    

    Would probably add a reasonable timeout.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • drg5577 2018-10-19 22:32

    The timeout you are referring to is most likely a script execution time limit [very common on shared hosting]

    And there is not much you can do about that sadly.

    However what you can do is [if you have control over the server where the script is called from] is

    1. send the request to the remote server [including a webhook callback url]
    2. Have the script do it's thing
    3. Have the script run the webhook to do the processing of the results

    offcourse this also has implications on how the processing/displaying should be handled but i do not have enough information to go into specifics in this answer.

    评论
    解决 无用
    打赏 举报
  • douhao1956 2018-10-19 22:50

    Do not attempt to execute long running scripts in a web page.

    If you need a response from another system and you have SSH access then seperate the invocation of the task and the collection of the results into 2 seperate steps (see link above and the discussion it links to for some hints on how to do the invocation). Put a timed redirect on the first page to the second.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题