Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

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)

like image 693
shashi Avatar asked Dec 11 '25 15:12

shashi


1 Answers

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.

like image 173
Stamos Avatar answered Dec 13 '25 05:12

Stamos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!