问题
Suppose I have a simple daemon type script that I run on my webserver. I run it in a terminal, with gnu screen, so I can keep an eye on it. That works fine (incidentally, I use this trick).
But now suppose I'd like to make a web page where I can keep an eye on my script's output. What's the easiest way to do that?
Notes:
- This is mainly for myself and a couple co-hackers so if websockets is the answer and it only works on Chrome or something, that's acceptable.
- This question is asking something similar: How to make all connected browsers reload initiated by a server-side event. But I'm hoping for a simpler, quick-and-dirty solution, and especially a general way to quickly do this for any script I might want to keep an eye on from a browswer.
回答1:
Let your daemon log output into a place that is accessible from a web script (Such as a database table), which can then display it. Have the webpage update once every few seconds. (You can get fancy at do it with XmlHttpRequest
and Javascript, if you want)
Edit:
Simplest way to have a page update, is with a meta http-equiv="refresh" content="5"> tag. A bit more elegant is to use Javascript - You can use something like PeriodicalUpdater for jQuery.
These solutions are both polling techniques. There is a trick you can pull to make the event propagate instantly, called comet. But that's a bit more complicated, and frankly a poll probably suits your use case fine.
回答2:
This looks really promising (thanks to my friend and hacker extraordinaire, David Yang):
http://blog.new-bamboo.co.uk/2009/12/7/real-time-online-activity-monitor-example-with-node-js-and-websocket
来源:https://stackoverflow.com/questions/2680129/whats-a-simple-way-to-web-ify-my-command-line-daemon