javascript 如何在 Node.js 中获得服务器正常运行时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28705009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the server uptime in Node.js?
提问by Inna
How do I get a server uptime in Node.js so I can output it by a command like;
如何在 Node.js 中获得服务器正常运行时间,以便我可以通过如下命令输出它;
if(commandCheck("/uptime")){
Give server uptime;
}
Now I don't know how to calculate the uptime from the server's startup.
现在我不知道如何计算服务器启动的正常运行时间。
采纳答案by Hakan Kose
What you can do to get normal time format is;
您可以做些什么来获得正常的时间格式;
String.prototype.toHHMMSS = function () {
var sec_num = parseInt(this, 10); // don't forget the second param
var hours = Math.floor(sec_num / 3600);
var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
var seconds = sec_num - (hours * 3600) - (minutes * 60);
if (hours < 10) {hours = "0"+hours;}
if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) {seconds = "0"+seconds;}
var time = hours+':'+minutes+':'+seconds;
return time;
}
if(commandCheck("/uptime")){
var time = process.uptime();
var uptime = (time + "").toHHMMSS();
console.log(uptime);
}
回答by loganfsmyth
You can use process.uptime()
. Just call that to get the number of seconds since node
was started.
您可以使用process.uptime()
. 只需调用它即可获取自node
启动以来的秒数。
function format(seconds){
function pad(s){
return (s < 10 ? '0' : '') + s;
}
var hours = Math.floor(seconds / (60*60));
var minutes = Math.floor(seconds % (60*60) / 60);
var seconds = Math.floor(seconds % 60);
return pad(hours) + ':' + pad(minutes) + ':' + pad(seconds);
}
var uptime = process.uptime();
console.log(format(uptime));
回答by melvinto
To get process's uptime in seconds
以秒为单位获取进程的正常运行时间
console.log(process.uptime())
To get OS uptime in seconds
以秒为单位获得操作系统正常运行时间
console.log(require('os').uptime())
回答by Matthew Daly
Assuming this is a *nix server, you can use the uptime
shell command using child_process
:
假设这是一个 *nix 服务器,您可以使用以下uptime
命令使用 shell 命令child_process
:
var child = require('child_process');
child.exec('uptime', function (error, stdout, stderr) {
console.log(stdout);
});
If you want to format that value differently or pass it through somewhere else, it should be trivial to do so.
如果您想以不同的方式格式化该值或将其传递到其他地方,那么这样做应该很简单。
EDIT: The definition of uptime seems a little unclear. This solution will tell the user how long the device has been powered on for, which may or may not be what you're after.
编辑:正常运行时间的定义似乎有点不清楚。此解决方案将告诉用户设备已开启多长时间,这可能是您想要的,也可能不是。
回答by Matt Harrison
I'm not sure if you're talking about an HTTP server, an actual machine (or VPS) or just a Node app in general. Here's an example for an http
server in Node though.
我不确定您是在谈论 HTTP 服务器、实际机器(或 VPS)还是一般的 Node 应用程序。不过,这里有一个http
Node 中的服务器示例。
Store the time when your server starts by getting Date.now()
inside the listen
callback. And then you can calculate uptime by subtracting this value from Date.now()
at another point in time.
当你的服务器开始通过获取存储时间Date.now()
内listen
回调。然后您可以通过从Date.now()
另一个时间点减去该值来计算正常运行时间。
var http = require('http');
var startTime;
var server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Uptime: ' + (Date.now() - startTime) + 'ms');
});
server.listen(3000, function () {
startTime = Date.now();
});
回答by Joseph Garrone
To get the unix uptime in ms syncronously:
要以毫秒为单位同步获得 Unix 正常运行时间:
const fs= require("fs");
function getSysUptime(){
return parseFloat(fs.readFileSync("/proc/uptime", {
"encoding": "utf8"
}).split(" ")[0])*1000;
}
console.log(getSysUptime());