新手问题 怎样启动一个进程而不受当前 ruby 进程影响

fsword · 2012年08月21日 · 最后由 googya 回复于 2012年08月21日 · 3541 次阅读

当前 ruby 进程有 bundle setup,所以如果启动的进程执行 ruby 脚本,就会遇到各种复杂情况,所以我需要创建一个进程,但是又能不受当前进程影响

把环境变量清空?

如何理解不受当前进程影响? shelling out 不就可以吗

#2 楼 @hooopo shelling out 是什么意思?

#3 楼 @fsword system("xx") 都算 shelling out

Shelling out When shelling out (using the system or backticks methods, for example), Bundler's environment changes will propogate to the subshell environment. If you desire to shell out without Bundler's environment changes, simply employ the with_clean_env method. It will restore all environment variables to what they were before Bundler was activated. For example:

Bundler.with_clean_env do
  `brew install wget`
end

#2 楼 @hooopo 举个例子—— 在项目路径下

$ ruby -e '`brakeman`' # 可执行
$ bundle exec ruby -e '`brakeman`' # 执行失败,因为Gemfile里面没有brakeman

而 brakeman 是根据管理员配置动态执行的脚本,不可能都放在 Gemfile 里面

#4 楼 @hooopo 恩,要的就是这个,多谢啦

#5 楼 @fsword 理论上像 brakeman、foreman 这样的不需要加载的程序也需要放到 gemfile。因为这些也是依赖

gem "brakeman", :require => false

#7 楼 @hooopo 这个一般不用,因为我这里的应用并不依赖它

2b 方法。换个目录执行...

#4 楼 @hooopo

顶 4 楼。又知道了个.with_clean_env, 不过能用到的情形实在是不多。

spawn 不是挺好的么?

需要 登录 后方可回复, 如果你还没有账号请 注册新账号