Waiting for a process to do its job in a node app

#What do I want to achieve

I have a snap which exposes a node app. The node app spawns a child process which is a shell script that in turn fires a binary. The binary captures observations from a sensor and writes them to a file in the SNAP_COMMON area. The binary is passed an argument “n” from the node app which tells the binary how many observations it has to write into the file. I have set a watch on the SNAP_COMMON area in the node app that watches for file events. When a event is caught indicating that the binary has updated the file with new readings, we read the file and call a POST API that posts the observation to a REST server. I want to close the watch when the spawned process(shell script) exits so that the node app exits gracefully and doesn’t remain watching the SNAP-COMMON area.

#What am I doing now

What I have done now is, I have edited the binary to do an “n+1” ^th write to create a file in the SNAP_COMMON area which will have a particular content eg. “Session Closed”. When I receive this I am closing the watch. In this case everything is working fine and the application exits gracefully.

#Rejection :frowning:

This design was rejected because it involved a check where a file was read to take a decision. The other idea I proposed was to update a variable to do the check but it was rejected at the same basis as before.

#Solution Idea :expressionless:

  1. I thought I could check for pid of the child process in the node app and when it exits,I would close the watch.
  2. I could set an environment variable that would be created in the shell script after the binary call that would be checked in the node app and then watch would be removed.

#HELP HELP HELP! :smiling_imp:

Suggest a way to do what I want to achieve or tell me if any of my ideas is feasible (rooting for #2 :raised_hands:)

Solution idea 1 feels like the most natural way to do this in general, and I bet node already exposes some high-level API to d that easily since it’s so common to wait for children to do their job.