Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the server uptime in Node.js?

How do I get a server uptime in Node.js so I can output it by a command like;

if(commandCheck("/uptime")){
  Give server uptime;
}

Now I don't know how to calculate the uptime from the server's startup.

like image 996
Inna Avatar asked Feb 24 '15 19:02

Inna


People also ask

How do I get uptime node JS?

You can use process. uptime() . Just call that to get the number of seconds since node was started.

What is process uptime?

The process. uptime() method is an inbuilt application programming interface of the process module which is used to get the number of seconds the Node. js process is running. Syntax: process.

What is Node JS run time?

The Node. js runtime is the software stack responsible for installing your web service's code and its dependencies and running your service. The Node.js runtime for App Engine in the standard environment is declared in the app.yaml file: Node.js 16.

How do I find my server name in Node JS?

To get the name or hostname of the OS, you can use the hostname() method from the os module in Node. js. /* Get hostname of os in Node. js */ // import os module const os = require("os"); // get host name const hostName = os.


1 Answers

You can use process.uptime(). Just call that to get the number of seconds since node was started.

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));
like image 197
loganfsmyth Avatar answered Oct 04 '22 16:10

loganfsmyth