Executing a System Command on Another Process by Using exec()

The simplest way to add work to another process from a Node.js process is to execute a system command in a subshell, using the exec() function. The exec() function can execute just about anything that can be executed from a console prompt, such as a binary executable, shell script, Python script, or batch file.

When executed, the exec() function creates a system subshell and then executes a command string in that shell just as if you had executed it from a console prompt. This gives you the advantage of being able to leverage the capabilities of a console shell, such as accessing environment variables, on the command line.

The syntax for the exec() function, which returns a ChildProcess ...

Get Node.js, MongoDB, and AngularJS Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.