3

I have a Node script and I need it to be executed as a single instance application. What I need is something in the form

// myscript.js
if(somelibmaybe.already_running())
  process.exit();
else
  // Do things

Is there any way at all to do this in a decent, cross-platform way?

Matteo Monti
  • 8,362
  • 19
  • 68
  • 114
  • The first thing that comes to mind is a lockfile type setup, but that could create problems if the process isn't able to clean up afterwards, say after a crash, in which the lockfile still exists, so the process would refuse to start, creating a difficult situation for normal users. – Brandon Anzaldi Apr 04 '16 at 18:49
  • Yup. That's my problem. Any workaround to that? – Matteo Monti Apr 04 '16 at 18:51
  • I found [`pidlock`](https://www.npmjs.com/package/pidlock) which seems to actually handle lockfiles in a sane way. (checking that the process is still alive in addition to checking the lockfile's existence) There's still a fair amount of caveats with it. (`node myscript.js` would count as a different application instance than `node myscript`. The command invocation must match for it to be effective.) – Brandon Anzaldi Apr 04 '16 at 21:07

0 Answers0