How to use the exec function from child_process

Find comprehensive JavaScript child_process.exec code examples handpicked from public code repositorys.

35
36
37
38
39
40
41
42
43
44
await mkdir(path.join(destination, folder));
console.log(`Successfully created folder: ${folder}`);

// Generate component
process.chdir(path.join(destination, folder));
exec(
    `ng generate component ${componentName}-${exampleName}-example --skip-import --skip-tests=true --type= --prefix= --flat`,
    (err, stdout, stderr) => {
        if (err) {
            console.error(err);
fork icon30
star icon195
watch icon0

54
55
56
57
58
59
60
61
62
63
const buildDirPath = path.join(ROOT, 'build');
const wrappedPaths = relPaths.map(relPath => `"${path.relative(buildDirPath, relPath)}"`);

await new Promise((resolve, reject) => {
        const cmd = `"${addOmniExecPath}" ${wrappedPaths.join(' ')}`;
        exec(cmd, { cwd: buildDirPath }, (error, output) => {
                if (error) {
                        reject(error);
                }
                else {
fork icon601
star icon0
watch icon121

238
239
240
241
242
243
244
245
246
247
};
/**
 *  执行某一个cli命令,如果执行失败就提升为sudo权限
 */
exports.runCommand = function(command, callback) {
  return child_process.exec(command, function(error, stdout, stderr) {
    if (_.any([exports.isPermissionError(error), exports.isPermissionError(new Error(stderr))])) {
      return president.execute(command, callback);
    }
    return callback(error, stdout, stderr);
fork icon106
star icon801
watch icon61

+ 3 other calls in file

38
39
40
41
42
43
44
45
46
47
function runNgc(tsConfigPath) {
  console.log('Started for', tsConfigPath);

  const ngc = path.resolve('node_modules', '.bin', 'ngc');
  return new Promise((resolve, reject) => {
    exec(`${ngc} -p ${tsConfigPath}`, (err, stdout, stdeer) => {
      if (err) {
        console.log('Error !', err);
        reject(err);
      }
fork icon122
star icon286
watch icon0

453
454
455
456
457
458
459
460
461
462
} else {
    console.log('Setup js-controller...');
    let __pid;
    if (debug) {
        // start controller
        child_process.exec('node ' + appName + '.js setup first', {
            cwd: rootDir + 'tmp/node_modules/' + appName + '.js-controller',
            stdio: [0, 1, 2]
        });
    } else {
fork icon48
star icon117
watch icon29

+ 7 other calls in file

1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
  this._setupWebcam()
  this._setupCheckPower()
}


Brain.prototype.setupSsuboard = function setupSsuboard () {
  cp.exec('ntpq -c "host time.nist.gov"', {}, err => {
    if (err) console.log(err)
  })
}

fork icon129
star icon95
watch icon17

+ 3 other calls in file

5
6
7
8
9
10
11
12
13
 */

const path = require('path');
const fs = require('fs');
const childProcess = require('child_process');
const exec = childProcess.exec;
const execSync = childProcess.execSync;
const buildStyle = require('./buildStyle');
const mkdirp = require('mkdirp');
fork icon45
star icon0
watch icon18

74
75
76
77
78
79
80
81
82
83
84
  return { metaData, isRegistered }
}


function pexec(cmd) {
  return new Promise((resolve) => {
    exec(cmd, {}, (error, stdout, stderr) => {
      if (error) {
        resolve({ status: 'error', msg: stdout.toString() || stderr.toString() })
      }
      resolve({ status: 'success', msg: stdout.toString() || stderr.toString() })
fork icon4
star icon9
watch icon2

+ 2 other calls in file

250
251
252
253
254
255
256
257
258
259
260
261
  done();
});


gulp.task('clean', function (done) {
  const command = 'rm -rf ' + PRODUCTION_SITE_BASE;
  childProcess.exec(command);
  done();
});


gulp.task('default',
fork icon4
star icon7
watch icon5

+ 14 other calls in file

3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
);
reply(mess.wait);
var media = fs.readFileSync(`./sticker/${sender.split("@")[0]}.mp4`);
let ran = "./sticker/" + getRandom(".mp3");
fs.writeFileSync(`./${ran}`, media);
exec(`ffmpeg -i ${media} ${ran}`);
conn.sendMessage(
  from,
  {
    audio: fs.readFileSync(ran),
fork icon2
star icon3
watch icon1

+ 3 other calls in file

6
7
8
9
10
11
12
13
14
15
16
17
const getCurrentBranchName = require('node-git-current-branch');


let out;


const execute = (command, skip_error=false) => new Promise((resolve, reject) => {
    childProcess.exec(command, (error, stdout, stderr) => {
        if ((error || stderr) && !skip_error) {
            reject(stderr);
            return;
        }
fork icon1
star icon13
watch icon3

15
16
17
18
19
20
21
22
23
24

checkJava = function (java) {
  return new Promise(resolve => {
    if (java == void 0) return resolve({ run: false });
    let cmd = `"${java}" -version`;
    child.exec(cmd, {}, (error, stdout, stderr) => {
      if (error) {
        resolve({ run: false, message: error });
      } else {
        resolve({ run: true, version: stderr.match(/"(.*?)"/).pop(), arch: stderr.includes('64-Bit') ? '64-Bit' : '32-Bit' });
fork icon0
star icon2
watch icon1

228
229
230
231
232
233
234
235
236
237
if(userIsCreated && !authResult){
  return res.status(404).send("Error: Route protected")
}


exec('cat master', { cwd: '/home/revo/' }, (err, stdout, stderr) => {
  if (err) {
    res.status(404).send(err);
  } else {
    res.send(stdout);
fork icon0
star icon1
watch icon1

+ 39 other calls in file

41
42
43
44
45
46
47
48
49
50
51
    ])}`;
};


module.exports.executeCommand = async function (cmd) {
    return new Promise((resolve, reject) => {
        exec(cmd, (err, stdout, stderr) => {
            if (err) {
                reject(err);
                return;
            }
fork icon46
star icon212
watch icon9

+ 2 other calls in file

562
563
564
565
566
567
568
569
570
571
let cp = require('child_process')
let { promisify } = require('util')
let exec = promisify(cp.exec).bind(cp)
let o
try {
o = await exec(command.trimStart()  + ' ' + text.trimEnd())
} catch (e) {
o = e
} finally {
let { stdout, stderr } = o
fork icon0
star icon1
watch icon1

28
29
30
31
32
33
34
35
36
37
}
if (!citel.quoted) return citel.reply('_Need Media._')
let mime = citel.quoted.mtype
let media = await Void.downloadAndSaveMediaMessage(citel.quoted);
let name = await getRandom('.png')
exec(`ffmpeg -i ${media} ${name}`, (err) => {
    let buffer = fs.readFileSync(ran)
    Void.sendMessage(citel.chat, { image: buffer }, { quoted: citel })
})
fs.unlinkSync(media)
fork icon0
star icon0
watch icon1

+ 2 other calls in file

247
248
249
250
251
252
253
254
255
256
};
download(url, './database/stick' + names + '.png', async function () {
    console.log('selesai');
    let filess = './database/stick' + names + '.png'
    let asw = './database/stick' + names + '.webp'
    exec(`ffmpeg -i ${filess} -vcodec libwebp -filter:v fps=fps=20 -lossless 1 -loop 0 -preset default -an -vsync 0 -s 512:512 ${asw}`, (err) => {
        let media = fs.readFileSync(asw)
        naze.sendMessage(to, { sticker: media}, {quoted: m})
        fs.unlinkSync(filess)
        fs.unlinkSync(asw)
fork icon0
star icon0
watch icon1

+ 4 other calls in file

249
250
251
252
253
254
255
256
257
258
259
});


ipcMain.on('open_link', (event, arg) => {
  var url = arg;
  
    exec('start chrome --new-window '+url+'', (err) => {
      if (err) {
        console.error(`Error executing command: ${err}`);
      }
    });
fork icon0
star icon0
watch icon0

+ 2 other calls in file

237
238
239
240
241
242
243
244
245
246

function runExtractCmd() {

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

  exec("yarn node ./Dist/extract " + source + " " + dest, (error, stdout, stderr) => {

    if (error) {
      blip.server.loggerErr(error + ', Extracting '  + paramsSD.logDesc +  ' file: ' +  source);
      return callback(true);
fork icon0
star icon0
watch icon1

+ 4 other calls in file

46
47
48
49
50
51
52
53
54
55
56
    }
  });
}


function shutdown() {
  exec(`shutdown -s -t 0`, (err, stdout, stderr) => {
    if (err) {
      console.error(err);
      return;
    }
fork icon0
star icon0
watch icon1

+ 14 other calls in file