I am trying to start a Ruby process on Windows using something like this:
p1 = spawn('ruby', 'loop.rb', [:out, :err] => ['process.log', "w"], :new_pgroup => true)
Then I also disconnect from the process via:
p1.detach
This should, as I understand it, create a new process that is independent of the parent. I even use the new_pgroup parameter to make sure that the new process gets its own process group.
When I execute my script, the subprocess starts and continues to work. The execution of the subprocess spawning script also ends. However, when I close the shell, the subprocess dies. I expect it to continue working (it works on OS X and Linux). I can't figure out if this is a bug in Ruby runtime on Windows or is this a limitation of Windows and how it processes processes.
For completeness, the full Ruby code of what I'm trying to do:
spawner.rb : can be done through ruby spawner.rb and just spawns a new subprocess. The process creates loop.rb, which is just an endless loop. Depending on the OS, it sets another parameter for creating a group of processes.
require "tempfile" require 'rbconfig' class SpawnTest def self.spawn_process if os == :windows p1 = spawn('ruby', 'loop.rb', [:out, :err] => ['process.log', "w"], :new_pgroup => true) else p1 = spawn('ruby', 'loop.rb', [:out, :err] => ['process.log', "w"], :pgroup => true) end
loop.rb
$stdout.sync = true $stderr.sync = true i = 0 while i < 10000 $stdout.puts "Iteration #{i}" sleep 1 i = i + 1 end $stdout.puts "Bye from #{Process.pid}"
I found a win32-process gem during my research. It seems to use win32 API calls for appearance processes. Does anyone know if this library will fix this problem?
Any help was appreciated.