I want to write a Node CLI that leaves a server/daemon running in the background, so subsequent calls to the CLI are much faster, as it can just print the latest info from the daemon (which is already primed with info as it's been watching the filesystem).

My purpose is not type-checking, but the CLI architecture I want is similar to how flow seems to work: to be able to launch the background service if it's not running (unique for the current working directory), or find the existing Node process if it's already running, and communicate with it either way. Looking at how flow-bin is set up in my local node_modules, it seems to ship with binaries (>20MB each) for the three main platforms (which I guess are all compiled from OCaml?). The flow CLI entry point is actually a tiny Node bin script which just spawns the relevant binary for the current platform. So I guess somehow the binary itself checks for an already-running instance of itself, and shuts itself down if found...? But I'm not sure how that could work, as it would need to still print something to stdout.

Can anyone shed any light on the architecture I want - basically, launching a detached child process from a parent Node process, and then reusing the same child process on subsequent executions of the parent? Are there any other open source examples I could look at, preferably just using plain Node JavaScript instead of OCaml?

1 Answer 1


The way to do this would be to use a file lock in your background program to check if it's already running. Here is a library that uses file locks to implement something called a named mutex. To make a program that only runs once you'd use it like this:

const NamedMutex = require('named-mutex')

const mutex = new NamedMutex('name_of_mutex_that_is_unique_to_my_program')

if(mutex.tryLock()) {
  //program is not already running, continue startup
else {
  //program is already running, don't startup  
  • Thank you, that explains part of it. But when the lockfile is present, I need to do more than just quit – I need to 'find' the already-running process, query it somehow, and print its response out to the terminal. I believe flow somehow does this. I guess one (manual) way would be to include some kind of identifier in the lockfile (PID?) so I can find the already-running process, and then query it, print results and exit. But I wondered if there's some cleverer way to do this, like somehow 'delegating' the stdin/stdout/stderr streams to the already-running background process.
    – callum
    Aug 3, 2020 at 16:50
  • The easiest way would be for the background process to listen on a tcp port on local host, and store that port number in the lockfile. I don't think there's any magic way to make this any easier. You can try googling for node IPC to find other ways for processes to communicate, but tcp is probably the easiest. You could do what you're proposing by spawning a child process, but child process will exit when parent exits, and you want to keep the background process running
    – Blake
    Aug 3, 2020 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.