exec module

Spawn child processes. Use the high-level run / run_with_input for one-shot commands; reach for spawn when you need full streaming control over stdin / stdout / stderr.

import "exec" as exec;

exec.run("git", ["rev-parse", "HEAD"], function (err, result) {
    if (err != null) { print("git failed:", err); return; }
    print("HEAD:", result.stdout);
});

High-level

FunctionDescription
exec.run(cmd, args, cb)Run to completion; cb(err, { code, stdout, stderr }).
exec.run_with_input(cmd, args, input, cb)Same, but feeds input to the child's stdin.

Low-level

FunctionDescription
exec.spawn(cmd, args, opts, on_exit) → procStart a child. opts: {cwd?, env?} or null. on_exit(code, signal).

Process object (from spawn)

  • proc.pid — child PID (number).
  • proc.stdin.write(data, cb?) — push bytes to stdin.
  • proc.stdin.close() — send EOF.
  • proc.stdout.on_data(fn) — receive stdout chunks.
  • proc.stdout.on_end(fn) — fires when the child closes stdout.
  • proc.stderr.on_data(fn) / proc.stderr.on_end(fn) — same for stderr.
  • proc.kill(sig?) — defaults to SIGTERM.

See also

  • cli — parse your own program's arguments.
  • sys — env vars, cwd, etc.