服务器重新启动时显示等待页面 [英] Show waiting page while server reboots

查看:123
本文介绍了服务器重新启动时显示等待页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台服务器并为其创建一个Web界面,如果用户按下页面上的重启按钮,则用户被重定向到 reboot.php 他在哪里应该看到一个微调器gif,直到服务器再次可以访问,服务器通过shell执行重新启动。如果服务器可以访问,那么我需要重定向到 main.php

I have a server and create a web interface for it, if a user presses the reboot button on the page, then the user is redirected to reboot.php where he is supposed to see a spinner gif till the server is reachable again and the server is getting rebooted via shell execute. If the server is reachable then i need to redirect to main.php

所以我创建了以下函数。该函数以5秒的超时开始,否则它将立即加载 main.php ,因为reboot命令需要时间。

So i created the following function. The function starts with a timeout of 5 seconds, because otherwise it would instantly load main.php because the reboot command takes it's time.

reboot.php

    $ret = false;

    test();

    function test()
    {
        setTimeout
        (
            function()
            {
                 $ret = ping("www.google.de");
                 if ($ret === false)
                 {
                     test();
                 }
                 else
                 {
                     window.location.href = "main.php";
                 }
            },
            3000
        );
    }

    function ping(url)
    {
        $.ajax
        (
            {
                url: url,
                success: function(result)
                {
                    alert('reply');
                    return true;
                },     
                error: function(result)
                {
                    alert('timeout/error');
                    return false;
                }
            }
        );
    }

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    <div class="col-lg-6 col-lg-offset-3" id="mydiv">
        <div class="mydiv_inhalt">
            <h2>Rebooting...</h2>
            <p>This can take about 2 minutes.</p>
            <center>
                <div class="waiting" id="waiting" style="margin-top:30px; margin-left: 0px;">
                    <center><img class="loading_table" src="http://www.securenet.com/sites/default/files/spinner.gif" alt="spinner gif"></center>
                </div>
            </center>
        </div>
    </div>

ajax.php

$cmd        = filter_input(INPUT_POST, "cmd");
if ( isset( $cmd ) && $cmd == "check_online_status" )
{
    echo "true";
}

在我的 ajax.php 如果通话成功,我只返回true。
然而,我的逻辑不起作用,似乎我的代码只试图调用我的 ajax.php ,然后再也没有尝试过我得到<控制台中的code> net :: ERR_CONNECTION_REFUSED 。

In my ajax.php i simply return "true" if the call was successful. However, my logic does not work, it seems that my code only tries to make one call to my ajax.php and then never tries again and i get net::ERR_CONNECTION_REFUSED in the console.

我在我的代码中添加了很多警报,但它们没有执行,所以我猜它不会尝试调用 ajax.php 获得 net :: ERR_CONNECTION_REFUSED 后第二次。

I put many alerts into my code, but they are not executed, so i guess it does not try to call ajax.php a second time after getting net::ERR_CONNECTION_REFUSED.

我唯一的想法是等待足够时间然后重定向到 main.php ,但这不是一个好的解决方案,因为需要运气,如果时间不够等怎么办。

My only idea would be to wait enough time and then redirect to main.php, but this is not a good solution because luck is needed, what if the time was not enough etc.

推荐答案

让我们总结一下代码中的问题:

Let's summarize the issues in your code:


  • 你需要什么使用 setInterval 每x秒执行一次函数

  • ajax请求是异步的,它应该保持不变。你不能期望回调的回报值,相反,你应该>
  • 回调
  • you need to use setInterval to execute the function every x seconds
  • the ajax request is asynchronous, and it should stay like that. You can't expect a return value from the callback, instead, you should act in the callback

生成的代码更简单:

var pingUrl = "www.google.de";
var targetUrl = "main.php";
setInterval(ping, 3000);

function ping() {
    $.ajax({
        url: pingUrl,
        success: function(result) {
            window.location.href = targetUrl;
        }
    });
}

这篇关于服务器重新启动时显示等待页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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