Node.js - is it possible to distinguish node instances by some additional parameter?
I am looking for the best way to distinguish Node.js instances on the processes list in a operating system in in a task manager. I run the same script many times that makes it complicated when I look on that list. I run Node.js instances from Java application many times. I need it to be able to select specific process using other application in operating system and monitor it, debug it and kill it if needed. I want to debug specific Node.js instances using IDE by attaching to existing processes.
Any ideas?
Consider to use environment variables.
e.g.
xxxxxxxxxx
INSTANCE_ID=10 node /path/to/script.js
But, for different operating systems, environment variables are set in different ways and some task managers may do not display them.
By running Node.js as a sub-process it may be good to use that technique:
Use some Node.js CLI options by setting unique values per instance.
e.g.
xxxxxxxxxx
node.exe --report-dir=reports/1 --diagnostic-dir=diagnostics/1 script.js
# ^ ^
# | |
Where: 1
means instance id.
Motivation: some operating systems tools do not display environment variables, e.g. default Task Manager in Microsoft Windows, so it may be good to use command line options.
Example source code (Java)
xxxxxxxxxx
// import java.util.List;
// import java.util.ArrayList;
final Runtime RUNTIME = Runtime.getRuntime();
int instanceId = 1;
List<String> command = new ArrayList<>();
command.add("node.exe");
command.add("--report-dir=reports/" + instanceId);
command.add("--diagnostic-dir=diagnostics/" + instanceId);
command.add("script.js");
Process process = RUNTIME.exec(command.toArray(String[]::new));