Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get time since current process was started in Node.js

Tags:

node.js

Using Node.js, how can I compute the time since the current process (the one running node) was started?

Ideally I'd like a cross-platform solution but a macOS-only solution would also be okay.

Unfortunately it is impossible for me to inject any code into Node.js startup. My library will only be loaded at some unknown point after the process starts.

like image 792
Alex Flint Avatar asked Dec 01 '16 01:12

Alex Flint


People also ask

Can I use setTimeout in node JS?

setTimeout() can be used to schedule code execution after a designated amount of milliseconds. This function is similar to window. setTimeout() from the browser JavaScript API, however a string of code cannot be passed to be executed.

What is process CWD ()?

process. cwd() returns the current working directory, i.e. the directory from which you invoked the node command. __dirname returns the directory name of the directory containing the JavaScript source code file.

What is process Stdin Nodejs?

The process. stdin property is an inbuilt application programming interface of the process module which listens for the user input. The stdin property of the process object is a Readable Stream. It uses on() function to listen for the event.


1 Answers

The function process.uptime() (available since v0.5.0) returns the number of seconds the current Node.js process has been running. I don't have a Mac to check, but there's no caveat in the documentation that it only works on certain platforms.

like image 60
K. A. Buhr Avatar answered Oct 06 '22 06:10

K. A. Buhr