开发者

How do I start a background process in Ruby?

开发者 https://www.devze.com 2023-02-23 12:28 出处:网络
I\'m having some difficulties to starting a background process in Ruby. I\'ve this code right now. #!/usr/bin/env ruby -w

I'm having some difficulties to starting a background process in Ruby.

I've this code right now.

#!/usr/bin/env ruby -w
require "daemons"
require 'rubygems'

path = "#{File.expand_path(File.dirname(__FILE__))}/.."

Daemons.run_proc('stalker',{
  :dir_mode => :normal,
  :dir => "#{path}/tmp/pids",
  :backtrace => true,
  :monitor => false,
  :log_output => true
}) do
  system "stalk #{path}/config/jobs.rb"
end

I then start the script using script/stalker start.

The problem is that I can't stop it. It saves the wrong PID into the pid file.

Like this:

script/stalker start
=> stalker: process with pid **39756** started.

ps aux | grep ruby 
=> linus    **39781**   0,3  1,9  2522752  78864   ??  S     8:39pm   0:10.11 ruby stalk script/../config/jobs.rb

Why doesn't the first pid match the one printed u开发者_如何转开发sing ps aux | grep ruby?

I've tried using exec, %x{} and this one system to run the script.


If you use run_proc, the code that you want daemonized should go in the block. Starting another process with system doesn't make sense (It will fork the process (giving you yet another pid), and then exec your jobs.rb script. Either move the code from jobs.rb into the run_proc block, or use Daemons.run

0

精彩评论

暂无评论...
验证码 换一张
取 消