0

我正在运行后台 rake 任务。(使用 '&')。问题是我希望它有时停止。所以我写了这个:

pinger_pid = system "ps | grep rake | awk '{print $1}'"
puts pinger_pid
system "kill -9 #{pinger_pid}"

似乎我得到了一个“真正的”输出垃圾!我怎样才能删除它?

输出:

ERROR: garbage process ID "true".
Usage:
  kill pid ...              Send SIGTERM to every process listed.
  kill signal pid ...       Send a signal to every process listed.
  kill -s signal pid ...    Send a signal to every process listed.
  kill -l                   List all signal names.
  kill -L                   List all signal names in a nice table.
  kill -l signal            Convert between signal numbers and names.
4

1 回答 1

1

系统返回真或假,取决于命令的成功与否。

使用 %x 捕获输出:

pinger_pid = %x(ps | grep rake | awk '{print $1}')
puts pinger_pid
system 'kill', '-9', pinger_pid
于 2012-08-27T18:03:41.517 回答