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 using 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 withsystemdoesn’t make sense (It willforkthe process (giving you yet another pid), and thenexecyour jobs.rb script. Either move the code fromjobs.rbinto therun_procblock, or use Daemons.run