开发者

Chromium process monitoring methods? Restart if crashes

I have开发者_如何学运维 a single page app written in pure HTML with no external plugings and using a full screen chromium for the client. (kiosk mode)

Though Chromium itself is very stable with very minor crashes, But I would like to have it restarted to the page, if it really crashes.

My thought this should be done with an external process like a watchdog, But how does the external monitor chrome? since there are several individual processes in the process table. Sometimes even it crashes, the process still alive out there.

Any suggestion or mature soutuion?


I just solved this myself. At first I tried using the processes API, but that isn't in the version of chrome I'm using on the kiosk. I've configured my kiosk with an extension that I wrote to handle other stuff (TUIO touch input, etc.), so I already had a place to add it.

My kiosk is running a little web server locally, because I found getting chromium to show file:// urls was just too much of a pain in the neck. If you are using file URLs, then your manifest will need to match those, instead of http URLs.

Here's the critical stuff from manifest.json:

"content_scripts": [
    {
        "matches": ["http://*/*"],
        "js": ["kiosk.js"]
    }
],

"background": {
    "scripts": [
        "background.js"
    ]
},

"permissions": [
    "webNavigation",
    "tabs",
    "runtime",
    "<all_urls>"
],

This goes into kiosk.js:

chrome.runtime.onMessage.addListener(
    function(message, sender, response) {
        response(message);
    }
);

Basically, it's a ping responder. If you send it a message, it sends it right back.

Here is background.js in its entirety:

var tab_id = -1;
var send_count = 0;
var recv_count = 0;

chrome.webNavigation.onBeforeNavigate.addListener(function (details) {
    tab_id = details.tabId;
});

setInterval(function() {
    if (tab_id == -1) return;
    if (send_count > recv_count+2) {
        chrome.tabs.reload(tab_id);
        send_count = recv_count = 0;
    }
    ++send_count;
    chrome.tabs.sendMessage(tab_id, "heartbeat", function(resp) {
        if (resp) {
            recv_count = send_count;
        }
    });
}, 1000);

It listens for my page showing up, and grabs the tab ID. It pings the responder. The documentation says that if an error happens, sendMessage will get called with no response, but that's not true. It actually doesn't get called at all. I coded it to handle either case.

Note that I originally said ++recv_count in the handler, but if you think about it, the above will be a little more robust to slowness on the receiving page.

Assuming your kiosk is linux (that is, you aren't insane), then you can test this easily enough by ssh'ing into your kiosk, and doing a ps axfww |grep render and then kill the first process listed. You'll see the sick computer screen for a sec, and then it will reload.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜