Node.js external child process communication sample

JS
S
JavaScript

Simple example of spanning a child process and reading the result of it.

1const exec = require('child_process').exec;
2
3exports.handler = (event, context, callback) => {
4    if (!event.cmd) {
5        return callback('Please specify a command to run as event.cmd');
6    }
7    const child = exec(event.cmd, (error) => {
8        // Resolve with result of process
9        callback(error, 'Process complete!');
10    });
11
12    // Log process stdout and stderr
13    child.stdout.on('data', console.log);
14    child.stderr.on('data', console.error);
15};
16

Created on 10/10/2018