Question

Possible Duplicate:
Spawn a background process in Ruby

Spent a couple days poking at this. I was using ruby 1.8.7 from the OS until recently. I would call a subshell with backticks. The subshell was a bash wrapper that would call run any program in the background with stdout and stderr both closed. It then ran disown to let init take over the process and it would return immediately. This worked great for years, where I would have this looping process kick off jobs in the background and immediately report back "yes, it ran, that's all I am telling you".

I upgraded everything to rvm 1.9.3 and everything's fine except for this trick. I'm starting to suspect it's more of a hack than I want to admit. In 1.9.3, I always get an EPIPE error when I spawn that subshell. It says that it has a broken pipe. I can accept that it isn't going to work in 1.9.3 since it seems kinda gross what I was doing in 1.8.7.

I've tried using the system command and I have tried open3:popen2. They also both throw an EPIPE with me calling the disown wrapper.

#!/bin/bash

# this will crash ruby if you keep trying to read from it.
$* >&- &

disown %1

This is the disown wrapper. In ruby I have something like

    r=`/usr/local/bin/disown /usr/local/bin/job.sh`

And when this runs, it throws

/usr/local/bin/runner.rb:88:in ``': Broken pipe (Errno::EPIPE)

If I don't assign the (zero) output to the r variable, the effect is identical. And with the system function and Open3:popen2.

So my goal is to simply run a command from ruby and not wait for it to come back. It takes several hours and I do not need to track it, just spawn it. I might try a worker thread pool if it begins to sound like ruby can no longer do this, or if my disown wrapper is too heinous to get any approval. Ok. Thanks.

*edit: Thanks for the great answers everyone. I think Casper showed me that if I had a better handle on the ruby lingo, I probably would have zeroed in on this. Sorry if this a little pedestrian. I appreciate the quick answers, everyone!

Was it helpful?

Solution 2

in Ruby 1.9.3, u can use

 Process.fork do
   # do your long time job
 end

OTHER TIPS

Well, you answered yourself: Process.spawn:

Process.spawn("something");

Check out the daemons gem. Then you can do:

require 'daemons'
Daemons.run('some_script.rb')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top