child_process.exec()

node v6.17.1
version: 1.0.0
endpointsharetweet
exec() spawns a shell and executes the command within that shell. Any generated output gets buffered. Unlike spawn(), the command is entered as a single string, without any array containing parameters.
// Require necessary modules const childProcess = require('child_process'); const os = require('os'); // Show OS/kernel version console.log(`Kernel version: ${os.release()}`); // Show current working directory (CWD) console.log(`CWD: ${process.cwd()}`);
Now creating instance of child_process.spawn() Goal is to show the contents of the root directory using native 'ls'-command.
const ls = childProcess.exec('ls -lh /', (err, stdout, stderr) => { if (err) { console.error(`An unexpected error happened: ${err}`); return; } console.log(`Info: ${stdout || 'No stdout present'}`); console.log(`Error: ${stderr || 'No error present'}`); });
ls.on('close', code => console.log(`Child process closed with code ${code}`));
Loading…

no comments

    sign in to comment