1

I am trying to run multiple ruby scripts simultaneously on my mac, and I'm not having any luck. I can see the the ruby processes start up, but then they immediately stop. The script works fine as a single process, no errors. Here are some examples of things I've tried.

10.times do

  system "nohup ruby program.rb \"arg1 arg2\" &"

end

10.times do

  `nohup ruby program.rb \"arg1 arg2\" &`

end


10.times do

  system "ruby program.rb \"arg1 arg2\""

end
1
  • 3
    How about using the Process class? Like the fork method? Commented Dec 2, 2015 at 22:45

3 Answers 3

2

Do you need to start it from ruby for any specific reason? Why don't you start it 10 times directly from bash? Like:

$ for i in seq 1 10; do nohup ruby foo.rb \&; done

Let me know..

Sign up to request clarification or add additional context in comments.

4 Comments

This solution doesn't address the nohup part of the question. I guess the effect would then be the same, but of course it would be a good idea to try it out.
Ok, now is addressing the nohup to don't append output to a tty, and escaping the '&'
no, starting it from ruby isn't a requirement, but i'm working with variables that are dynamically updating so i figured based would be more trouble it iterate a loop with a lot of vars. this would be more simple without vars, but still doubtful it wouldn't just kill each ruby process.
@user1934428 is exactly right, this solution may not solve the problem of my ruby processes just being killed immediately, but this is essentially the same command minus being invoked by ruby.
1

nohup redirects its output to a file $HOME/nohup.out, unless it is explicitly redirected. You should redirect the output of each invocation to a different file.

Also, for the safe side, I would redirect stdin to /dev/null - just in case the called program reads from stdin.

10.times do |i|

   system "nohup ruby program.rb 'arg1 arg2' </dev/null >#{ENV['HOME']}/nohup#{i}.out &"

end

BTW (and off topic): Are you sure, that you want to pass arg1 arg2 as a SINGLE argument to program.rb?

4 Comments

the arguments are coming out fine when evaluated with ARGV[i]. Is this dangerous? should i send an array object instead? These args I'm sending in are vars, I just simplified for the sake of the simplifying the problem.
what i find particularly useful about this answer is that you suggested how to route the output, which was another question I had, thanks! +1
fyi i tried this and it didn't write any files to nohup1.out etc... also checked ruby processes it appears it didn't even start. not sure what's going on
@botbot: Did you check the return code of system? Another possibility would be to wrap (just for testing) the whole nohup-line into a shell script and trace the execution of the shell script (with set -x).
1

You can build a solution with fork, exec and wait of the module Process.

# start child processes
10.times { fork { exec(cmd) } }
# wait for child processes
10.times { |pid| Process.wait }

Or a bit longer to play around with (Tested with Ruby 1.8.7 on Ubuntu). Added rescue nil to suppress error when waiting.

10.times do |i|
  fork do
    ruby_cmd = "sleep(#{10-i});puts #{i}"
    exec("ruby -e \"#{ruby_cmd}\"")
  end
end

10.times { Process.wait rescue nil }
puts "Finished!"

2 Comments

i keep getting this error: Errno::ECHILD: No child processes
@botbot The error is thrown if the child already finished. Putting rescue behind the waitpid suppresses this behavoir.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.