{
  "source": "doc/api/child_process.markdown",
  "modules": [
    {
      "textRaw": "Child Process",
      "name": "child_process",
      "stability": 3,
      "stabilityText": "Stable",
      "desc": "<p>Node provides a tri-directional <code>popen(3)</code> facility through the\n<code>child_process</code> module.\n\n</p>\n<p>It is possible to stream data through a child&apos;s <code>stdin</code>, <code>stdout</code>, and\n<code>stderr</code> in a fully non-blocking way.\n\n</p>\n<p>To create a child process use <code>require(&apos;child_process&apos;).spawn()</code> or\n<code>require(&apos;child_process&apos;).fork()</code>.  The semantics of each are slightly\ndifferent, and explained below.\n\n</p>\n",
      "classes": [
        {
          "textRaw": "Class: ChildProcess",
          "type": "class",
          "name": "ChildProcess",
          "desc": "<p><code>ChildProcess</code> is an <code>EventEmitter</code>.\n\n</p>\n<p>Child processes always have three streams associated with them. <code>child.stdin</code>,\n<code>child.stdout</code>, and <code>child.stderr</code>.  These may be shared with the stdio\nstreams of the parent process, or they may be separate stream objects\nwhich can be piped to and from.\n\n</p>\n<p>The ChildProcess class is not intended to be used directly.  Use the\n<code>spawn()</code> or <code>fork()</code> methods to create a Child Process instance.\n\n</p>\n",
          "events": [
            {
              "textRaw": "Event:  'exit'",
              "type": "event",
              "name": "exit",
              "params": [],
              "desc": "<p>This event is emitted after the child process ends. If the process terminated\nnormally, <code>code</code> is the final exit code of the process, otherwise <code>null</code>. If\nthe process terminated due to receipt of a signal, <code>signal</code> is the string name\nof the signal, otherwise <code>null</code>.\n\n</p>\n<p>See <code>waitpid(2)</code>.\n\n</p>\n"
            }
          ],
          "properties": [
            {
              "textRaw": "`stdin` {Stream object} ",
              "name": "stdin",
              "desc": "<p>A <code>Writable Stream</code> that represents the child process&apos;s <code>stdin</code>.\nClosing this stream via <code>end()</code> often causes the child process to terminate.\n\n</p>\n<p>If the child stdio streams are shared with the parent, then this will\nnot be set.\n\n</p>\n"
            },
            {
              "textRaw": "`stdout` {Stream object} ",
              "name": "stdout",
              "desc": "<p>A <code>Readable Stream</code> that represents the child process&apos;s <code>stdout</code>.\n\n</p>\n<p>If the child stdio streams are shared with the parent, then this will\nnot be set.\n\n</p>\n"
            },
            {
              "textRaw": "`stderr` {Stream object} ",
              "name": "stderr",
              "desc": "<p>A <code>Readable Stream</code> that represents the child process&apos;s <code>stderr</code>.\n\n</p>\n<p>If the child stdio streams are shared with the parent, then this will\nnot be set.\n\n</p>\n"
            },
            {
              "textRaw": "`pid` {Integer} ",
              "name": "pid",
              "desc": "<p>The PID of the child process.\n\n</p>\n<p>Example:\n\n</p>\n<pre><code>var spawn = require(&apos;child_process&apos;).spawn,\n    grep  = spawn(&apos;grep&apos;, [&apos;ssh&apos;]);\n\nconsole.log(&apos;Spawned child pid: &apos; + grep.pid);\ngrep.stdin.end();</code></pre>\n"
            }
          ],
          "methods": [
            {
              "textRaw": "child.kill([signal])",
              "type": "method",
              "name": "kill",
              "signatures": [
                {
                  "params": [
                    {
                      "textRaw": "`signal` {String} ",
                      "name": "signal",
                      "type": "String",
                      "optional": true
                    }
                  ]
                },
                {
                  "params": [
                    {
                      "name": "signal",
                      "optional": true
                    }
                  ]
                }
              ],
              "desc": "<p>Send a signal to the child process. If no argument is given, the process will\nbe sent <code>&apos;SIGTERM&apos;</code>. See <code>signal(7)</code> for a list of available signals.\n\n</p>\n<pre><code>var spawn = require(&apos;child_process&apos;).spawn,\n    grep  = spawn(&apos;grep&apos;, [&apos;ssh&apos;]);\n\ngrep.on(&apos;exit&apos;, function (code, signal) {\n  console.log(&apos;child process terminated due to receipt of signal &apos;+signal);\n});\n\n// send SIGHUP to process\ngrep.kill(&apos;SIGHUP&apos;);</code></pre>\n<p>Note that while the function is called <code>kill</code>, the signal delivered to the child\nprocess may not actually kill it.  <code>kill</code> really just sends a signal to a process.\n\n</p>\n<p>See <code>kill(2)</code>\n\n</p>\n"
            },
            {
              "textRaw": "child.send(message, [sendHandle])",
              "type": "method",
              "name": "send",
              "signatures": [
                {
                  "params": [
                    {
                      "textRaw": "`message` {Object} ",
                      "name": "message",
                      "type": "Object"
                    },
                    {
                      "textRaw": "`sendHandle` {Handle object} ",
                      "name": "sendHandle",
                      "type": "Handle object",
                      "optional": true
                    }
                  ]
                },
                {
                  "params": [
                    {
                      "name": "message"
                    },
                    {
                      "name": "sendHandle",
                      "optional": true
                    }
                  ]
                }
              ],
              "desc": "<p>Send a message (and, optionally, a handle object) to a child process.\n\n</p>\n<p>See <code>child_process.fork()</code> for details.\n\n</p>\n"
            }
          ]
        }
      ],
      "methods": [
        {
          "textRaw": "child_process.spawn(command, [args], [options])",
          "type": "method",
          "name": "spawn",
          "signatures": [
            {
              "return": {
                "textRaw": "return: {ChildProcess object} ",
                "name": "return",
                "type": "ChildProcess object"
              },
              "params": [
                {
                  "textRaw": "`command` {String} The command to run ",
                  "name": "command",
                  "type": "String",
                  "desc": "The command to run"
                },
                {
                  "textRaw": "`args` {Array} List of string arguments ",
                  "name": "args",
                  "type": "Array",
                  "desc": "List of string arguments",
                  "optional": true
                },
                {
                  "textRaw": "`options` {Object} ",
                  "options": [
                    {
                      "textRaw": "`cwd` {String} Current working directory of the child process ",
                      "name": "cwd",
                      "type": "String",
                      "desc": "Current working directory of the child process"
                    },
                    {
                      "textRaw": "`customFds` {Array} **Deprecated** File descriptors for the child to use for stdio.  (See below) ",
                      "name": "customFds",
                      "type": "Array",
                      "desc": "**Deprecated** File descriptors for the child to use for stdio.  (See below)"
                    },
                    {
                      "textRaw": "`env` {Object} Environment key-value pairs ",
                      "name": "env",
                      "type": "Object",
                      "desc": "Environment key-value pairs"
                    }
                  ],
                  "name": "options",
                  "type": "Object",
                  "optional": true
                }
              ]
            },
            {
              "params": [
                {
                  "name": "command"
                },
                {
                  "name": "args",
                  "optional": true
                },
                {
                  "name": "options",
                  "optional": true
                }
              ]
            }
          ],
          "desc": "<p>Launches a new process with the given <code>command</code>, with  command line arguments in <code>args</code>.\nIf omitted, <code>args</code> defaults to an empty Array.\n\n</p>\n<p>The third argument is used to specify additional options, which defaults to:\n\n</p>\n<pre><code>{ cwd: undefined,\n  env: process.env\n}</code></pre>\n<p><code>cwd</code> allows you to specify the working directory from which the process is spawned.\nUse <code>env</code> to specify environment variables that will be visible to the new process.\n\n</p>\n<p>Example of running <code>ls -lh /usr</code>, capturing <code>stdout</code>, <code>stderr</code>, and the exit code:\n\n</p>\n<pre><code>var util  = require(&apos;util&apos;),\n    spawn = require(&apos;child_process&apos;).spawn,\n    ls    = spawn(&apos;ls&apos;, [&apos;-lh&apos;, &apos;/usr&apos;]);\n\nls.stdout.on(&apos;data&apos;, function (data) {\n  console.log(&apos;stdout: &apos; + data);\n});\n\nls.stderr.on(&apos;data&apos;, function (data) {\n  console.log(&apos;stderr: &apos; + data);\n});\n\nls.on(&apos;exit&apos;, function (code) {\n  console.log(&apos;child process exited with code &apos; + code);\n});</code></pre>\n<p>Example: A very elaborate way to run &apos;ps ax | grep ssh&apos;\n\n</p>\n<pre><code>var util  = require(&apos;util&apos;),\n    spawn = require(&apos;child_process&apos;).spawn,\n    ps    = spawn(&apos;ps&apos;, [&apos;ax&apos;]),\n    grep  = spawn(&apos;grep&apos;, [&apos;ssh&apos;]);\n\nps.stdout.on(&apos;data&apos;, function (data) {\n  grep.stdin.write(data);\n});\n\nps.stderr.on(&apos;data&apos;, function (data) {\n  console.log(&apos;ps stderr: &apos; + data);\n});\n\nps.on(&apos;exit&apos;, function (code) {\n  if (code !== 0) {\n    console.log(&apos;ps process exited with code &apos; + code);\n  }\n  grep.stdin.end();\n});\n\ngrep.stdout.on(&apos;data&apos;, function (data) {\n  console.log(data);\n});\n\ngrep.stderr.on(&apos;data&apos;, function (data) {\n  console.log(&apos;grep stderr: &apos; + data);\n});\n\ngrep.on(&apos;exit&apos;, function (code) {\n  if (code !== 0) {\n    console.log(&apos;grep process exited with code &apos; + code);\n  }\n});</code></pre>\n<p>Example of checking for failed exec:\n\n</p>\n<pre><code>var spawn = require(&apos;child_process&apos;).spawn,\n    child = spawn(&apos;bad_command&apos;);\n\nchild.stderr.setEncoding(&apos;utf8&apos;);\nchild.stderr.on(&apos;data&apos;, function (data) {\n  if (/^execvp\\(\\)/.test(data)) {\n    console.log(&apos;Failed to start child process.&apos;);\n  }\n});</code></pre>\n<p>Note that if spawn receives an empty options object, it will result in\nspawning the process with an empty environment rather than using\n<code>process.env</code>. This due to backwards compatibility issues with a deprecated\nAPI.\n\n</p>\n<p>There is a deprecated option called <code>customFds</code> which allows one to specify\nspecific file descriptors for the stdio of the child process. This API was\nnot portable to all platforms and therefore removed.\nWith <code>customFds</code> it was possible to hook up the new process&apos; <code>[stdin, stdout,\nstderr]</code> to existing streams; <code>-1</code> meant that a new stream should be created.\nUse at your own risk.\n\n</p>\n<p>There are several internal options. In particular <code>stdinStream</code>,\n<code>stdoutStream</code>, <code>stderrStream</code>. They are for INTERNAL USE ONLY. As with all\nundocumented APIs in Node, they should not be used.\n\n</p>\n<p>See also: <code>child_process.exec()</code> and <code>child_process.fork()</code>\n\n</p>\n"
        },
        {
          "textRaw": "child_process.exec(command, [options], callback)",
          "type": "method",
          "name": "exec",
          "signatures": [
            {
              "return": {
                "textRaw": "Return: ChildProcess object ",
                "name": "return",
                "desc": "ChildProcess object"
              },
              "params": [
                {
                  "textRaw": "`command` {String} The command to run, with space-separated arguments ",
                  "name": "command",
                  "type": "String",
                  "desc": "The command to run, with space-separated arguments"
                },
                {
                  "textRaw": "`options` {Object} ",
                  "options": [
                    {
                      "textRaw": "`cwd` {String} Current working directory of the child process ",
                      "name": "cwd",
                      "type": "String",
                      "desc": "Current working directory of the child process"
                    },
                    {
                      "textRaw": "`customFds` {Array} **Deprecated** File descriptors for the child to use for stdio.  (See below) ",
                      "name": "customFds",
                      "type": "Array",
                      "desc": "**Deprecated** File descriptors for the child to use for stdio.  (See below)"
                    },
                    {
                      "textRaw": "`env` {Object} Environment key-value pairs ",
                      "name": "env",
                      "type": "Object",
                      "desc": "Environment key-value pairs"
                    },
                    {
                      "textRaw": "`encoding` {String} (Default: 'utf8') ",
                      "name": "encoding",
                      "default": "utf8",
                      "type": "String"
                    },
                    {
                      "textRaw": "`timeout` {Number} (Default: 0) ",
                      "name": "timeout",
                      "default": "0",
                      "type": "Number"
                    },
                    {
                      "textRaw": "`maxBuffer` {Number} (Default: 200*1024) ",
                      "name": "maxBuffer",
                      "default": "200*1024",
                      "type": "Number"
                    },
                    {
                      "textRaw": "`killSignal` {String} (Default: 'SIGTERM') ",
                      "name": "killSignal",
                      "default": "SIGTERM",
                      "type": "String"
                    }
                  ],
                  "name": "options",
                  "type": "Object",
                  "optional": true
                },
                {
                  "textRaw": "`callback` {Function} called with the output when process terminates ",
                  "options": [
                    {
                      "textRaw": "`error` {Error} ",
                      "name": "error",
                      "type": "Error"
                    },
                    {
                      "textRaw": "`stdout` {Buffer} ",
                      "name": "stdout",
                      "type": "Buffer"
                    },
                    {
                      "textRaw": "`stderr` {Buffer} ",
                      "name": "stderr",
                      "type": "Buffer"
                    }
                  ],
                  "name": "callback",
                  "type": "Function",
                  "desc": "called with the output when process terminates"
                }
              ]
            },
            {
              "params": [
                {
                  "name": "command"
                },
                {
                  "name": "options",
                  "optional": true
                },
                {
                  "name": "callback"
                }
              ]
            }
          ],
          "desc": "<p>Runs a command in a shell and buffers the output.\n\n</p>\n<pre><code>var util = require(&apos;util&apos;),\n    exec = require(&apos;child_process&apos;).exec,\n    child;\n\nchild = exec(&apos;cat *.js bad_file | wc -l&apos;,\n  function (error, stdout, stderr) {\n    console.log(&apos;stdout: &apos; + stdout);\n    console.log(&apos;stderr: &apos; + stderr);\n    if (error !== null) {\n      console.log(&apos;exec error: &apos; + error);\n    }\n});</code></pre>\n<p>The callback gets the arguments <code>(error, stdout, stderr)</code>. On success, <code>error</code>\nwill be <code>null</code>.  On error, <code>error</code> will be an instance of <code>Error</code> and <code>err.code</code>\nwill be the exit code of the child process, and <code>err.signal</code> will be set to the\nsignal that terminated the process.\n\n</p>\n<p>There is a second optional argument to specify several options. The\ndefault options are\n\n</p>\n<pre><code>{ encoding: &apos;utf8&apos;,\n  timeout: 0,\n  maxBuffer: 200*1024,\n  killSignal: &apos;SIGTERM&apos;,\n  cwd: null,\n  env: null }</code></pre>\n<p>If <code>timeout</code> is greater than 0, then it will kill the child process\nif it runs longer than <code>timeout</code> milliseconds. The child process is killed with\n<code>killSignal</code> (default: <code>&apos;SIGTERM&apos;</code>). <code>maxBuffer</code> specifies the largest\namount of data allowed on stdout or stderr - if this value is exceeded then\nthe child process is killed.\n\n\n</p>\n"
        },
        {
          "textRaw": "child_process.execFile(file, args, options, callback)",
          "type": "method",
          "name": "execFile",
          "signatures": [
            {
              "return": {
                "textRaw": "Return: ChildProcess object ",
                "name": "return",
                "desc": "ChildProcess object"
              },
              "params": [
                {
                  "textRaw": "`file` {String} The filename of the program to run ",
                  "name": "file",
                  "type": "String",
                  "desc": "The filename of the program to run"
                },
                {
                  "textRaw": "`args` {Array} List of string arguments ",
                  "name": "args",
                  "type": "Array",
                  "desc": "List of string arguments"
                },
                {
                  "textRaw": "`options` {Object} ",
                  "options": [
                    {
                      "textRaw": "`cwd` {String} Current working directory of the child process ",
                      "name": "cwd",
                      "type": "String",
                      "desc": "Current working directory of the child process"
                    },
                    {
                      "textRaw": "`customFds` {Array} **Deprecated** File descriptors for the child to use for stdio.  (See below) ",
                      "name": "customFds",
                      "type": "Array",
                      "desc": "**Deprecated** File descriptors for the child to use for stdio.  (See below)"
                    },
                    {
                      "textRaw": "`env` {Object} Environment key-value pairs ",
                      "name": "env",
                      "type": "Object",
                      "desc": "Environment key-value pairs"
                    },
                    {
                      "textRaw": "`encoding` {String} (Default: 'utf8') ",
                      "name": "encoding",
                      "default": "utf8",
                      "type": "String"
                    },
                    {
                      "textRaw": "`timeout` {Number} (Default: 0) ",
                      "name": "timeout",
                      "default": "0",
                      "type": "Number"
                    },
                    {
                      "textRaw": "`maxBuffer` {Number} (Default: 200*1024) ",
                      "name": "maxBuffer",
                      "default": "200*1024",
                      "type": "Number"
                    },
                    {
                      "textRaw": "`killSignal` {String} (Default: 'SIGTERM') ",
                      "name": "killSignal",
                      "default": "SIGTERM",
                      "type": "String"
                    }
                  ],
                  "name": "options",
                  "type": "Object"
                },
                {
                  "textRaw": "`callback` {Function} called with the output when process terminates ",
                  "options": [
                    {
                      "textRaw": "`error` {Error} ",
                      "name": "error",
                      "type": "Error"
                    },
                    {
                      "textRaw": "`stdout` {Buffer} ",
                      "name": "stdout",
                      "type": "Buffer"
                    },
                    {
                      "textRaw": "`stderr` {Buffer} ",
                      "name": "stderr",
                      "type": "Buffer"
                    }
                  ],
                  "name": "callback",
                  "type": "Function",
                  "desc": "called with the output when process terminates"
                }
              ]
            },
            {
              "params": [
                {
                  "name": "file"
                },
                {
                  "name": "args"
                },
                {
                  "name": "options"
                },
                {
                  "name": "callback"
                }
              ]
            }
          ],
          "desc": "<p>This is similar to <code>child_process.exec()</code> except it does not execute a\nsubshell but rather the specified file directly. This makes it slightly\nleaner than <code>child_process.exec</code>. It has the same options.\n\n\n</p>\n"
        },
        {
          "textRaw": "child_process.fork(modulePath, [args], [options])",
          "type": "method",
          "name": "fork",
          "signatures": [
            {
              "return": {
                "textRaw": "Return: ChildProcess object ",
                "name": "return",
                "desc": "ChildProcess object"
              },
              "params": [
                {
                  "textRaw": "`modulePath` {String} The module to run in the child ",
                  "name": "modulePath",
                  "type": "String",
                  "desc": "The module to run in the child"
                },
                {
                  "textRaw": "`args` {Array} List of string arguments ",
                  "name": "args",
                  "type": "Array",
                  "desc": "List of string arguments",
                  "optional": true
                },
                {
                  "textRaw": "`options` {Object} ",
                  "options": [
                    {
                      "textRaw": "`cwd` {String} Current working directory of the child process ",
                      "name": "cwd",
                      "type": "String",
                      "desc": "Current working directory of the child process"
                    },
                    {
                      "textRaw": "`customFds` {Array} **Deprecated** File descriptors for the child to use for stdio.  (See below) ",
                      "name": "customFds",
                      "type": "Array",
                      "desc": "**Deprecated** File descriptors for the child to use for stdio.  (See below)"
                    },
                    {
                      "textRaw": "`env` {Object} Environment key-value pairs ",
                      "name": "env",
                      "type": "Object",
                      "desc": "Environment key-value pairs"
                    },
                    {
                      "textRaw": "`encoding` {String} (Default: 'utf8') ",
                      "name": "encoding",
                      "default": "utf8",
                      "type": "String"
                    },
                    {
                      "textRaw": "`timeout` {Number} (Default: 0) ",
                      "name": "timeout",
                      "default": "0",
                      "type": "Number"
                    }
                  ],
                  "name": "options",
                  "type": "Object",
                  "optional": true
                }
              ]
            },
            {
              "params": [
                {
                  "name": "modulePath"
                },
                {
                  "name": "args",
                  "optional": true
                },
                {
                  "name": "options",
                  "optional": true
                }
              ]
            }
          ],
          "desc": "<p>This is a special case of the <code>spawn()</code> functionality for spawning Node\nprocesses. In addition to having all the methods in a normal ChildProcess\ninstance, the returned object has a communication channel built-in. The\nchannel is written to with <code>child.send(message, [sendHandle])</code> and messages\nare received by a <code>&apos;message&apos;</code> event on the child.\n\n</p>\n<p>For example:\n\n</p>\n<pre><code>var cp = require(&apos;child_process&apos;);\n\nvar n = cp.fork(__dirname + &apos;/sub.js&apos;);\n\nn.on(&apos;message&apos;, function(m) {\n  console.log(&apos;PARENT got message:&apos;, m);\n});\n\nn.send({ hello: &apos;world&apos; });</code></pre>\n<p>And then the child script, <code>&apos;sub.js&apos;</code> might look like this:\n\n</p>\n<pre><code>process.on(&apos;message&apos;, function(m) {\n  console.log(&apos;CHILD got message:&apos;, m);\n});\n\nprocess.send({ foo: &apos;bar&apos; });</code></pre>\n<p>In the child the <code>process</code> object will have a <code>send()</code> method, and <code>process</code>\nwill emit objects each time it receives a message on its channel.\n\n</p>\n<p>By default the spawned Node process will have the stdin, stdout, stderr\nassociated with the parent&apos;s.\n\n</p>\n<p>These child Nodes are still whole new instances of V8. Assume at least 30ms\nstartup and 10mb memory for each new Node. That is, you cannot create many\nthousands of them.\n\n</p>\n<p>The <code>sendHandle</code> option to <code>child.send()</code> is for sending a handle object to\nanother process. Child will receive the handle as as second argument to the\n<code>message</code> event. Here is an example of sending a handle:\n\n</p>\n<pre><code>var server = require(&apos;net&apos;).createServer();\nvar child = require(&apos;child_process&apos;).fork(__dirname + &apos;/child.js&apos;);\n// Open up the server object and send the handle.\nserver.listen(1337, function() {\n  child.send({ server: true }, server._handle);\n});</code></pre>\n<p>Here is an example of receiving the server handle and sharing it between\nprocesses:\n\n</p>\n<pre><code>process.on(&apos;message&apos;, function(m, serverHandle) {\n  if (serverHandle) {\n    var server = require(&apos;net&apos;).createServer();\n    server.listen(serverHandle);\n  }\n});</code></pre>\n"
        }
      ],
      "type": "module",
      "displayName": "Child Process"
    }
  ]
}
