ruby 如何获取当前 rake 任务的 PID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9509102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:50:40  来源:igfitidea点击:

How to get PID of current rake task?

rubyshellrake

提问by ylluminate

I'm putting in a reaper line into a rake task to kill some additionally spawned ruby tasks as they somehow creep up on occasion.

我正在将收割者线放入一个耙子任务中,以杀死一些额外产生的 ruby​​ 任务,因为它们有时会以某种方式爬行。

system "ps aux | grep 'namespace:taskname' | grep ruby | grep -v grep | awk '{print }' | xargs kill -9; echo 'Reaped old namespace:taskname processes.'"

I'd like to add grep -v $PID_OF_CURRENT_TASKin that just to be sure I don't kill the current task that's running as well.

我想添加grep -v $PID_OF_CURRENT_TASK它只是为了确保我不会终止正在运行的当前任务。

How do I get that PID?

我如何获得那个PID?

回答by Linuxios

You get the current PID in Ruby with Process.pid

您可以使用 Ruby 获取当前的 PID Process.pid