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.
现在我不知道如何计算服务器启动时的正常运行时间。
6 个解决方案
#1
3
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);
}
#2
11
You can use process.uptime()
. Just call that to get the number of seconds since node
was started.
您可以使用process.uptime()。只需调用它来获取自节点启动以来的秒数。
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));
#3
5
To get process's uptime in seconds
在几秒钟内获得进程的正常运行时间
console.log(process.uptime())
To get OS uptime in seconds
在几秒钟内获得OS正常运行时间
console.log(require('os').uptime())
#4
3
Assuming this is a *nix server, you can use the uptime
shell command using child_process
:
假设这是一个* nix服务器,您可以使用child_process使用uptime shell命令:
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.
编辑:正常运行时间的定义似乎有点不清楚。此解决方案将告诉用户设备已打开多长时间,这可能是也可能不是您所追求的。
#5
1
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应用程序。以下是Node中http服务器的示例。
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.
通过在listen回调中获取Date.now()来存储服务器启动的时间。然后,您可以通过在另一个时间点从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();
});
#6
0
To get the unix uptime in ms syncronously:
以ms为单位同步获取unix正常运行时间:
const fs= require("fs");
function getSysUptime(){
return parseFloat(fs.readFileSync("/proc/uptime", {
"encoding": "utf8"
}).split(" ")[0])*1000;
}
console.log(getSysUptime());
#1
3
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);
}
#2
11
You can use process.uptime()
. Just call that to get the number of seconds since node
was started.
您可以使用process.uptime()。只需调用它来获取自节点启动以来的秒数。
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));
#3
5
To get process's uptime in seconds
在几秒钟内获得进程的正常运行时间
console.log(process.uptime())
To get OS uptime in seconds
在几秒钟内获得OS正常运行时间
console.log(require('os').uptime())
#4
3
Assuming this is a *nix server, you can use the uptime
shell command using child_process
:
假设这是一个* nix服务器,您可以使用child_process使用uptime shell命令:
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.
编辑:正常运行时间的定义似乎有点不清楚。此解决方案将告诉用户设备已打开多长时间,这可能是也可能不是您所追求的。
#5
1
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应用程序。以下是Node中http服务器的示例。
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.
通过在listen回调中获取Date.now()来存储服务器启动的时间。然后,您可以通过在另一个时间点从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();
});
#6
0
To get the unix uptime in ms syncronously:
以ms为单位同步获取unix正常运行时间:
const fs= require("fs");
function getSysUptime(){
return parseFloat(fs.readFileSync("/proc/uptime", {
"encoding": "utf8"
}).split(" ")[0])*1000;
}
console.log(getSysUptime());