2017-03-21 54 views
1

如果您不熟悉icecast,它是一个多媒体服务器。gulp-exec子进程立即关闭

当我在终端中运行icecast -c ./icecast/icecast.xml时,它会启动一个保持活动状态的icecast服务器。

所以我想在我的node.js进程旁边运行该命令,每次运行gulp

我将以下内容添加到我的gulp文件中。

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`) 
    }) 
}) 

当我在终端运行命令gulp icecast,一饮而尽说Starting 'icecast'...,然后立即终止。没有一个回调火。我真的很喜欢它,直到我cmd-c吞噬过程。

我觉得我错过了一些关于吞咽如何工作的基本知识,但是在吞咽(或吞咽-exec)文档中我找不到任何提及此类主题的任何内容。

回答

0

我有一个非常类似的问题,并注意到其警告,如果你只是想运行一个命令,你应该使用节点的child_process.execgulp-exec page has a note

运行exec(command)立即退出该过程;然而,当我试图在一饮而尽,Exec的页面中指定的语法,其中包括一个回调:

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); 
    }); 
}) 

现在保存过程活着。我并不需要输出和错误输出,所以设法逃脱:

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

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

我不是在节点精通,公正地分享对我工作;我希望这个解决方案解决您的问题。