Answers for "node exec"

6

node run shell command

// You can use 'exec' this way

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

exec("ls -la", (error, stdout, stderr) => {
    if (error) {
        console.log(`error: ${error.message}`);
        return;
    }
    if (stderr) {
        console.log(`stderr: ${stderr}`);
        return;
    }
    console.log(`stdout: ${stdout}`);
});
Posted by: Guest on April-20-2020
0

node exec

// Async
const { exec } = require("child_process"); // import { exec } from "child_process"

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

// Sync
const { execSync } = require("child_process"); // import { execSync } from "child_process"

const result = execSync("ls -la")
console.log(result.toString())
Posted by: Guest on July-07-2021
2

node spawn

const { spawn } = require('child_process');
const ls = spawn('ls', ['-lh', '/usr']);

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

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

ls.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
});
Posted by: Guest on July-10-2020
0

how to return when child process is complete in node js

var child = require('child_process').exec('python celulas.py')
child.stdout.pipe(process.stdout)
child.on('exit', function() {
  process.exit()
})
Posted by: Guest on June-11-2020
0

child process

// OR...
const { exec, spawn } = require('child_process');
exec('my.bat', (err, stdout, stderr) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log(stdout);
});

// Script with spaces in the filename:
const bat = spawn('"my script.cmd"', ['a', 'b'], { shell: true });
// or:
exec('"my script.cmd" a b', (err, stdout, stderr) => {
  // ...
});
Posted by: Guest on April-07-2020
0

how to return when child process is complete in node js

var execSync = require('exec-sync');

var user = execSync('python celulas.py');
Posted by: Guest on June-11-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language