Home  Nodejs   Child_proce ...

child_process module example in nodejs

Node.js provides the child_process module to handle the creation of new processes. The child_process module allows you to spawn new processes, execute shell commands, and communicate with the child processes via standard input/output streams.

Here are some common methods provided by the child_process module to launch new processes:

1. spawn

The spawn function launches a new process with a given command. It returns a ChildProcess object which can be used to interact with the spawned process.

const { spawn } = require('child_process');

const child = spawn('ls', ['-lh', '/usr']);

child.stdout.on('data', (data) => {
    console.log(`stdout: ${data}`);
});

child.stderr.on('data', (data) => {
    console.error(`stderr: ${data}`);
});

child.on('close', (code) => {
    console.log(`child process exited with code ${code}`);
});

2. exec

The exec function runs a command in a shell and buffers the output. It is useful for executing shell commands that return a large amount of output.

const { exec } = require('child_process');

exec('ls -lh /usr', (error, stdout, stderr) => {
    if (error) {
        console.error(`exec error: ${error}`);
        return;
    }
    console.log(`stdout: ${stdout}`);
    console.error(`stderr: ${stderr}`);
});

3. execFile

The execFile function is similar to exec but does not run the command in a shell, making it slightly more efficient and secure when you do not need shell features like wildcard expansion, variable substitution, etc.

const { execFile } = require('child_process');

execFile('node', ['--version'], (error, stdout, stderr) => {
    if (error) {
        console.error(`execFile error: ${error}`);
        return;
    }
    console.log(`stdout: ${stdout}`);
    console.error(`stderr: ${stderr}`);
});

4. fork

The fork function is a special case of spawn that runs a new instance of the Node.js process. It is primarily used for creating child processes that run Node.js scripts. It returns a ChildProcess object.

const { fork } = require('child_process');

const child = fork('child_script.js');

child.on('message', (message) => {
    console.log('Message from child:', message);
});

child.send({ hello: 'world' });

Example: Spawning a New Process

Here's an example that demonstrates how to spawn a new process using the spawn method from the child_process module:

const { spawn } = require('child_process');

const child = spawn('node', ['-v']);

child.stdout.on('data', (data) => {
    console.log(`Child Process stdout: ${data}`);
});

child.stderr.on('data', (data) => {
    console.error(`Child Process stderr: ${data}`);
});

child.on('close', (code) => {
    console.log(`Child Process exited with code ${code}`);
});

In this example, a new Node.js process is spawned to execute the command node -v, which prints the Node.js version. The output from the child process is captured and printed to the console. The exit code of the child process is also logged when it closes.

The child_process module provides flexible and powerful methods to manage the creation and interaction with child processes in Node.js, allowing you to execute shell commands, run scripts, and create complex workflows.

Published on: Jun 19, 2024, 02:33 AM  
 

Comments

Add your comment