shashi
shashi

Reputation: 4696

Can I detect whether a child process has finished io processing on exit?

In my program I am spawning a child process which is doing some I/O work. I have subscribe to the exit event for the child process. However, the file which is being written still seems to be being used after the exit event has fired.

From the nodejs docs https://nodejs.org/api/child_process.html#child_process_class_childprocess

Note that when the 'exit' event is triggered, child process stdio streams might still be open.

How can I detect that the file processing has completed? I have tried using lsof but it takes a lot of time (around 10 seconds to run once, and I end up executing it multiple times as in the first instant, the file may not be free)

Upvotes: 3

Views: 1838

Answers (1)

Stamos
Stamos

Reputation: 3998

You can you use close event to check when all stdio streams have closed.

Per Node.js v9.2.1 Documentation - ChildProcess

Event: 'close'

  • code The exit code if the child exited on its own.
  • signal The signal by which the child process was terminated.

The 'close' event is emitted when the stdio streams of a child process have been closed. This is distinct from the 'exit' event, since multiple processes might share the same stdio streams.

Upvotes: 2

Related Questions