是否可以在监视任务后运行任务?

时间:2013-07-17 21:27:11

标签: gruntjs grunt-contrib-watch

我有一个基于PHP的项目,不能在grunt-php上运行。相反,我使用grunt-exec运行我的MAMP服务器进行开发。

exec: {
  serverup: {
    command: '/Applications/MAMP/bin/start.sh'
  },
  serverdown: {
    command: '/Applications/MAMP/bin/stop.sh'
  }
}

在我的自定义开发任务中,我在监视任务之前运行MAMP启动脚本。然后,我在退出监视任务后试图停止MAMP服务器。

grunt.registerTask('default', ['jshint', 'concat', 'compass:dev', 'exec:serverup', 'watch', 'exec:serverdown']);

但是,如果我使用Ctrl-C退出任务,exec:serverdown任务似乎永远不会运行。有没有办法让这项工作?由于服务器永远不会出现故障,因此在我手动运行停止脚本之前,该端口会被占用,如果我在将其关闭之前尝试再次运行默认任务,则会出现错误。

如果没有,还有其他方法可以完成同样的事情吗?

1 个答案:

答案 0 :(得分:4)

您可以在SIGINT上收听并运行脚本:

var exec = require('child_process').exec;
process.on('SIGINT', function () {
    exec('/Applications/MAMP/bin/stop.sh', function () {
        process.exit();
    });
});

module.exports = function (grunt) {};