我正在尝试从ruby启动一个grunt任务。此任务将永远运行,因为它将启动服务器。
在后面的ruby脚本中,我想关闭我用grunt启动的服务器。
我现在有以下内容:
grunt_proxy_pid = spawn("TARGET_PORT=#{port+1} PROXY_PORT=#{port} grunt server:test", :out=>"/dev/null")
Process.detach grunt_proxy_pid
... ruby code ...
Process.kill "SIGINT", grunt_proxy_pid
然而,这不会终止grunt任务,只会终止执行grunt server:test
命令的shell命令(在任务管理器中,pid为'grunt_proxy_pid'的任务类似于sh -c TARGET_PORT=3523 PROXY_PORT=3224 grunt server:test
),但是grunt进程本身有另一个pid。
如何获取grunt task pid以便终止grunt task?
事实证明,以这种方式设置环境变量是个问题。这样设置它们:
grunt_proxy_pid = spawn({
"TARGET_PORT" => "#{port+1}",
"PROXY_PORT" => "#{port}"
}, "grunt server:test", :out=>"/dev/null")
Process.detach grunt_proxy_pid
这样grunt命令不会以sh -c
开始,而grunt_proxy_pid
是grunt进程本身的pid。