我正在使用 daemon kit 启动一个后台 ruby 进程来侦听 Amazon SQS 消息。一旦收到一条消息,它就会使用需要在 JRuby 中运行的 Open3.popen3 启动一个子进程。
后台进程需要在 MRI 中运行,因为 daemon kit 使用 Process.daemon 来守护进程。但到目前为止,我还不能强制子进程在 JRuby 中运行。
我正在使用 rbenv 来管理 ruby 版本,所以起初我认为这会起作用:
Open3.popen3({"RUBY_VERSION" => "jruby-1.7.8"}, "rp5 run /path/to/sketch.rb") do |stdin, stdout, stderr, wait_thr|
# read stderr and stdout for status and error information ....
end
但是在子进程输出中我收到错误:“rbenv: jruby: command not found”
然后我跟踪了 rbenv 如何运行它的可执行文件,以便我可以绕过 rbenv 并直接在 JRuby 中运行 rp5 可执行文件。
我首先在文件夹中找到了 rp5 可执行文件:~/.rbenv/versions/jruby-1.7.8/bin/rp5
#!/Users/fede/.rbenv/versions/jruby-1.7.8/bin/jruby
#
# This file was generated by RubyGems.
#
# The application 'ruby-processing' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'rubygems'
version = ">= 0"
if ARGV.first
str = ARGV.first
str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
if str =~ /\A_(.*)_\z/
version = $1
ARGV.shift
end
end
gem 'ruby-processing', version
load Gem.bin_path('ruby-processing', 'rp5', version)
然后我执行 Gem.bin_path 方法来查找它正在调用的 rp5 可执行文件。在 gem 里面: ~/.rbenv/versions/jruby-1.7.8/lib/ruby/gems/shared/gems/ruby-processing-2.3.1/bin/rp5 然后我尝试通过调用运行子进程这个 rp5 可执行文件直接:
Open3.popen3("~/.rbenv/versions/jruby-1.7.8/lib/ruby/gems/shared/gems/ruby-processing-2.3.1/bin/rp5 run path/to/sketch.rb") do |stdin, stdout, stderr, wait_thr|
# read stderr and stdout for status and error information ....
end
但我仍然收到相同的“未找到 jruby 命令”错误。
然后我检查了那个可执行文件:
#!/usr/bin/env ruby
file = __FILE__
if test(?l, file)
require "pathname"
file = Pathname.new(file).realpath
end
require File.expand_path(File.dirname(file) + "/../lib/ruby-processing")
Processing::Runner.execute
那么顶部的 shebang 是否意味着该可执行文件正在使用默认的 ruby 版本?
甚至可以在完全不同的 ruby 版本中启动子进程吗?
谢谢。