Ruby压制系统真输出

时间:2012-08-27 17:59:53

标签: ruby-on-rails ruby ruby-on-rails-3

我正在运行后台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.

1 个答案:

答案 0 :(得分:1)

系统返回true或false,具体取决于命令的成功。

使用%x捕获输出:

pinger_pid = %x(ps | grep rake | awk '{print $1}')
puts pinger_pid
system 'kill', '-9', pinger_pid