title: Node and talking to child processes
body: <h2>Context</h2>
  <p>Writing a serial monitor to listen to an Arduino isn't hard. Starting it with command line params for port name and baudrate is straightforward, and thus it's not complicated to configure and launch the monitor from Node:</p>
  <pre><code>child_process.exec(&quot;./monitor COM6 115200&quot;);
  </code></pre>
  <p>This question concerns <code>exec</code>, <code>fork</code> and <code>spawn</code> which are similar but with subtleties I do not yet grasp. As well as launch with parameters, I need to</p>
  <ul>
  <li>Capture the output so I can display it in a window</li>
  <li>Kill the child process
  <ul>
  <li>to re-launch with different parameters</li>
  <li>to flash the Arduino and then re-launch after it reboots</li>
  </ul>
  </li>
  </ul>
  <p>I used netcore to write a console app that takes two command line arguments, then connects and listens, echoing to its <code>stdout</code>. I chose netcore because it can run on all three platforms.</p>
  <h2>Questions</h2>
  <p>Which should I use of <code>exec</code>, <code>fork</code> and <code>spawn</code>?</p>
  <p>How do I kill the child process?</p>
  <p>How do I capture the <code>stdout</code> of the child process?</p>
  <p>Node documentation talks about a <a href="https://nodejs.org/api/child_process.html#child_process_subprocess_kill_signal" rel="nofollow noreferrer">kill method on a <code>subprocess</code> object</a>. The sample code on that page implies this object is returned by <code>spawn</code> but when I use spawn it seems to fail silently. That or it's working but I don't know what I'm doing, which is why I'm posing this question.</p>
  <p>All of this Node code will be part of a VSCode extension, so if you also know about those I'd like to pipe stdout to a VSCode OutputChannel if that's possible.</p>