Javascript Grunt运行try catch并在出现错误时运行shell命令

Javascript Grunt运行try catch并在出现错误时运行shell命令,javascript,bash,shell,gruntjs,grunt-contrib-watch,Javascript,Bash,Shell,Gruntjs,Grunt Contrib Watch,我希望grunt“捕获”grunt错误并运行shell命令,然后重试 这里有一个特定的用例 在这个错误之后 [4mRunning "connect:livereload" (connect) task[24m] [31mFatal error: Port 9000 is already in use by another process.[39m] [Finished in 13.4s with exit code 1] 我想运行以下shell命令 lsof -P | grep ':9000'

我希望grunt“捕获”grunt错误并运行shell命令,然后重试

这里有一个特定的用例

在这个错误之后

[4mRunning "connect:livereload" (connect) task[24m]
[31mFatal error: Port 9000 is already in use by another process.[39m]
[Finished in 13.4s with exit code 1]
我想运行以下shell命令

lsof -P | grep ':9000' | awk '{print $2}' | xargs kill -9
要在重试之前终止“正在使用的端口”

grunt exec
将允许我执行shell命令,但我还没有找到可以捕获此错误的插件(或其他方法)

[4mRunning "connect:livereload" (connect) task[24m]
[31mFatal error: Port 9000 is already in use by another process.[39m]
[Finished in 13.4s with exit code 1]
我欢迎任何使用grunt或shell来解决这个问题的想法