【问题标题】:How to get the pid for the first task with the Daemons gem?如何使用 Daemons gem 获取第一个任务的 pid?
【发布时间】:2012-06-24 06:25:42
【问题描述】:

我正在尝试使用 Daemons gem 运行同一代码/脚本的多个实例。我一直在 IRB 会话中使用它,但似乎无法获得我正在寻找的功能。我想多次运行相同的脚本来跟踪 PID,以便我可以手动启动和停止进程。更具体地说,我有一个 rails 模型,其实例将控制单个进程,因此我需要使用类似的东西来启动和停止它

mydaemon = MyDaemon.create
mydaemon.start # starts the process
mydaemon.stop  # stops the process

但是,为了实现这样的目的,我想将进程的 PID 存储在活动记录中。当我在 IRB 中运行以下代码时:

require 'rubygems'        # if you use RubyGems
require 'daemons'

task1 = Daemons.call(:multiple => true) do
  loop {
    File.open("/tmp/daemon_test.log", "w") {|f| f.write(Time.now.to_s + "\n")}
    sleep 5
  }
end

任务会自动启动(无需在 task1 上调用 start),它不会返回 pid,而是返回与 task1 关联的 Daemons::Application 对象。我只有在启动 task1 的第二个实例时才能访问 pid,即:

task1.start
 => 574 # PID of process but now I have 2 proc's running, one of which I don't know the PID of

我是否使用了守护程序错误?还是没有办法获取调用 Daemons.call 时自动启动的第一个进程的 PID?

谢谢, 托梅克

【问题讨论】:

  • 我不希望守护程序代码在irb 中很好地发挥作用。我建议改为通过脚本进行测试。
  • @sarnold 看起来运行脚本会产生相同的结果gist.github.com/2969149

标签: ruby fork pid multiple-instances daemons


【解决方案1】:

docs say 表示 Daemons::Application 对象有一个公共属性 pid 返回一个 Daemons::Pid 对象,因此请尝试使用该 task1.pid.pid

【讨论】:

猜你喜欢
  • 2012-03-19
  • 2010-10-07
  • 2012-05-08
  • 2012-06-30
  • 1970-01-01
  • 2022-11-24
  • 2015-04-01
  • 1970-01-01
  • 2015-11-01
相关资源
最近更新 更多