list-open-files
lsof
for Node.js, with an asynchronous Promise-based API and JSON-serializable self-explained objects.
API
lsof opts?: Options : Promise< Array< ProcessInfo > >;
The optional options object is defined as:
If no pids
are provided, the current process will be queried.
If concurrency
is specified, it will be used to call the underlying lsof
command with that concurrency (at most). The default is built-in and environment specific - it depends on the underlying hardware and is optimized, and therefore generally a good choice. To affect the host computer the least, specify 1
, although this will have a very small effect.
The return value is eventually an array (even if only one pid is queried) of zero or more ProcessInfo
objects each on the form:
Example
All open files in a primitive Node.js application
lsof .then console.log result ;
When run on MacOS as node . > output.txt
, will write a file called output.txt
(note fd 1)
View `output.txt`
Listening and client connections
The following application creates a listening port (which will happen to listen on IPv6, which also supports IPv4). It then connects to it, which results on two more sockets, one for the outgoing client socket, one for the incoming socket since the application connects to itself.
testIP .catch console.error err ;
This will output:
fd: 22 type: 'IP' access: 'u' lock: ' ' size: undefined iNode: undefined linkCount: undefined name: '*:50710' protocol: 'TCP' version: 6 from: undefined to: address: '*' port: 50710 fd: 22 type: 'IP' access: 'u' lock: ' ' size: undefined iNode: undefined linkCount: undefined name: '*:50710' protocol: 'TCP' version: 6 from: undefined to: address: '*' port: 50710 fd: 26 type: 'IP' access: 'u' lock: ' ' size: undefined iNode: undefined linkCount: undefined name: '127.0.0.1:50711->127.0.0.1:50710' protocol: 'TCP' version: 4 from: address: '127.0.0.1' port: 50711 to: address: '127.0.0.1' port: 50710 fd: 28 type: 'IP' access: 'u' lock: ' ' size: undefined iNode: undefined linkCount: undefined name: '127.0.0.1:50710->127.0.0.1:50711' protocol: 'TCP' version: 6 from: address: '127.0.0.1' port: 50710 to: address: '127.0.0.1' port: 50711
Reference
ProcessInfo
Process
InternalFile
File
UnknownFile
FileDescriptorIP
IP