Phil
Phil

Reputation: 137

gulp-exec child process closing immediately

If you're not familiar with icecast, it's a multimedia server.

When I run icecast -c ./icecast/icecast.xml in the terminal, it starts an icecast server, which stays alive.

So I want to run that command alongside my node.js process, every time I run gulp.

I added the following to my gulpfile.

import exec from 'gulp-exec'

...

const icecastDir = path.resolve(`${__dirname}/icecast/`)

...

gulp.task(`icecast`, () => {
  return exec(`/usr/local/bin/icecast -c ${icecastDir}/icecast.xml`)
    .on(`data`, () => {
      console.log(`data`)
    }) 
    .on(`error`, () => {
      console.log(`error`)
    }) 
    .on(`end`, () => {
      console.log(`end`)
    }) 
    .on(`close`, () => {
      console.log(`error`)
    }) 
    .on(`readable`, () => {
      console.log(`readable`)
    }) 
})

When I run the command gulp icecast in my terminal, gulp says Starting 'icecast'... and then immediately terminates. None of the callbacks fire. I'd really like it to stay alive until I cmd-c the gulp process.

I feel like I'm missing some fundamental knowledge about how gulp works, but I can't find anything mentioning such topics in the gulp (or gulp-exec) docs.

Upvotes: 1

Views: 1153

Answers (1)

rlafuente
rlafuente

Reputation: 1984

I had a very similar issue, and noticed that the gulp-exec page has a note which warns that, if you just want to run a command, you should use Node's child_process.exec.

Running exec(command) immediately exited the process; however, when I tried the syntax specified in gulp-exec's page, which includes a callback:

var exec = require('child_process').exec;

gulp.task('task', function (cb) {
  exec('ping localhost', function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
})

It now kept the process alive. I didn't need stdout and stderr output, so managed to get away with:

var exec = require('child_process').exec;

gulp.task('task', function (cb) {
  exec('ping localhost', function (e) { cb(e); });
})

I'm not versed in Node, just sharing what worked for me; I hope this solution addresses your problem.

Upvotes: 1

Related Questions