# child\_process

***

**1. Spawning a Child Process**

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

const child = spawn('ls', ['-la']);

// Handle child process output
child.stdout.on('data', (data) => console.log(`stdout: ${data}`));
child.stderr.on('data', (data) => console.error(`stderr: ${data}`));

// Handle child process exit
child.on('close', (code) => code === 0 ? console.log('Child process exited successfully') : console.error(`Child process exited with code ${code}`));
```

**2. Executing a Command Synchronously**

```js
const { execSync } = require('child_process');

try {
  const output = execSync('ls -la').toString();
  console.log(`stdout: ${output}`);
} catch (err) {
  console.error(`stderr: ${err.stderr}`);
}
```

**3. Executing a Command Asynchronously**

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

exec('ls -la', (err, stdout, stderr) => {
  if (err) console.error(`stderr: ${err.stderr}`);
  if (stderr) console.error(`stderr: ${stderr}`);
  console.log(`stdout: ${stdout}`);
});
```

**4. Spawning a Child Process with Input and Output Pipes**

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

// Create stdin, stdout, and stderr pipes
const stdin = require('stream').Writable();
const stdout = require('stream').Readable();
const stderr = require('stream').Readable();

// Spawn child process with specified pipes
const child = spawn('wc', ['-l'], {
  stdio: [stdin, stdout, stderr],
});

// Write data to child process stdin
stdin.write('Line 1\nLine 2\nLine 3\n');
stdin.end();

// Handle child process output
stdout.on('data', (data) => console.log(`stdout: ${data}`));
stderr.on('data', (data) => console.error(`stderr: ${data}`));

// Handle child process exit
child.on('close', (code) => code === 0 ? console.log('Child process exited successfully') : console.error(`Child process exited with code ${code}`));
```

**5. Sending Signals to a Child Process**

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

const child = spawn('sleep', ['10']);

// Kill child process after 5 seconds
setTimeout(() => child.kill('SIGINT'), 5000);
```

**6. Spawning a Child Process with Environment Variables**

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

// Create environment variables object
const env = {
  MY_VAR: 'Hello',
  ANOTHER_VAR: 'World',
};

// Spawn child process with specified environment variables
const child = spawn('echo', [], { env });

// Handle child process output
child.stdout.on('data', (data) => console.log(`stdout: ${data}`));
child.stderr.on('data', (data) => console.error(`stderr: ${data}`));

// Handle child process exit
child.on('close', (code) => code === 0 ? console.log('Child process exited successfully') : console.error(`Child process exited with code ${code}`));
```

**7. Spawning a Child Process with Working Directory**

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

// Spawn child process with specified working directory
const child = spawn('ls', [], { cwd: '/tmp' });

// Handle child process output
child.stdout.on('data', (data) => console.log(`stdout: ${data}`));
child.stderr.on('data', (data) => console.error(`stderr: ${data}`));

// Handle child process exit
child.on('close', (code) => code === 0 ? console.log('Child process exited successfully') : console.error(`Child process exited with code ${code}`));
```

**8. Piping Child Process Output to Another Process**

```js
const { spawn } = require('child_process');
const fs = require('fs');

// Spawn child process with stdout piped to file
const child = spawn('ls', [], {
  stdio: ['ignore', fs.openSync('output.txt', 'w'), 'ignore'],
});

// Handle child process exit
child.on('close', (code) => code === 0 ? console.log('Child process exited successfully') : console.error(`Child process exited with code ${code}`));
```

**9. Handling Child Process Events**

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

// Spawn child process with event handlers
const child = spawn('ls', [], {
  stdio: ['ignore', 'pipe', 'pipe'],
});

// Handle child process exit event
child.on('exit', (code, signal) => {
  if (code === 0) {
    console.log('Child process exited successfully');
  } else if (signal) {
    console.error(`Child process exited due to signal ${signal}`);
  } else {
    console.error(`Child process exited with code ${code}`);
  }
});

// Handle child process error event
child.on('error', (err) => console.error(`Child process error: ${err.message}`));

// Handle child process disconnect event
child.on('disconnect', () => console.log('Child process disconnected'));

// Handle child process unhandled error event
process.on('unhandledRejection', (reason, promise) => {
  if (promise.child === child) {
    console.error(`Child process unhandled error: ${reason.message}`);
  }
});
```

**10. Killing a Child Process**

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

// Spawn child process
const child = spawn('sleep', ['10']);

// Kill child process after 5 seconds
setTimeout(() => child.kill(), 5000);
```

**11. Waiting for Child Process to Exit**

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

// Spawn child process
const child = spawn('sleep', ['10']);

// Wait for child process to exit
child.on('exit', (code) => {
  if (code === 0) {
    console.log('Child process exited successfully');
  } else {
    console.error(`Child process exited with code ${code}`);
  }
});
```

**12. Executing a Command with Arguments**

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

// Execute command with arguments
exec('ls -la', (err, stdout, stderr) => {
  if (err) {
    console.error(`stderr: ${err.stderr}`);
    return;
  }
  if (stderr) {
    console.error(`stderr: ${stderr}`);
    return;
  }
  console.log(`stdout: ${stdout}`);
});
```

**13. Executing a Command with Options**

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

// Execute command with options
exec('ls -la', { maxBuffer: 20 * 1024 }, (err, stdout, stderr) => {
  if (err) {
    console.error(`stderr: ${err.stderr}`);
    return;
  }
  if (stderr) {
    console.error(`stderr: ${stderr}`);
    return;
  }
  console.log(`stdout: ${stdout}`);
});
```

**14. Executing a Command with Input**

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

// Execute command with input
exec('node', ['-e', 'console.log("Hello World!")'], (err, stdout, stderr) => {
  if (err) {
    console.error(`stderr: ${err.stderr}`);
    return;
  }
  if (stderr) {
    console.error(`stderr: ${stderr}`);
    return;
  }
  console.log(`stdout: ${stdout}`);
});
```

**15. Executing a Command with Environment Variables**

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

// Create environment variables object
const env = {
  MY_VAR: 'Hello',
  ANOTHER_VAR: 'World',
};

// Execute command with environment variables
exec('echo $MY_VAR $ANOTHER_VAR', { env }, (err, stdout, stderr) => {
  if (err) {
    console.error(`stderr: ${err.stderr}`);
    return;
  }
  if (stderr) {
    console.error(`stderr: ${stderr}`);
    return;
  }
  console.log(`stdout: ${stdout}`);
});
```

**16. Executing a Command with Working Directory**

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

// Execute command with working directory
exec('ls -la', { cwd: '/tmp' }, (err, stdout, stderr) => {
  if (err) {
    console.error(`stderr: ${err.stderr}`);
    return;
  }
  if (stderr) {
    console.error(`stderr: ${stderr}`);
    return;
  }
  console.log(`stdout: ${stdout}`);
});
```

**17. Executing a Command with Shell**

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

// Execute command with shell
exec('ls -la', { shell: '/bin/bash' }, (err, stdout, stderr) => {
  if (err) {
    console.error(`stderr: ${err.stderr}`);
    return;
  }
  if (stderr) {
    console.error(`stderr: ${stderr}`);
    return;
  }
  console.log(`stdout: ${stdout}`);
});
```

**18. Executing a Command with Stdio**

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

// Execute command with stdio
exec('ls -la', { stdio: 'pipe' }, (err, stdout, stderr) => {
  if (err) {
    console.error(`stderr: ${err.stderr}`);
    return;
  }
  if (stderr) {
    console.error(`stderr: ${stderr}`);
    return;
  }
  console.log(`stdout: ${stdout}`);
});
```

**19. Executing a Command with Timeout**

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

// Execute command with timeout
exec('ls -la', { timeout: 2000 }, (err, stdout, stderr) => {
  if (err) {
    console.error(`stderr: ${err.stderr}`);
    return;
  }
  if (stderr) {
    console.error(`stderr: ${stderr}`);
    return;
  }
  console.log(`stdout: ${stdout

```
