百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

childprocess

> 编程语言
开源

用于管理子进程的跨平台 Ruby 库。

597 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

用于管理子进程的跨平台 Ruby 库。

childprocess

This gem aims at being a simple and reliable solution for controlling external programs running in the background on any Ruby / OS combination.

The code originated in the selenium-webdriver gem, but should prove useful as a standalone library.

Requirements

  • Ruby 2.4+, JRuby 9+

Usage

The object returned from ChildProcess.build will implement ChildProcess::AbstractProcess.

Basic examples

…

Advanced examples

Output to pipe

…

Note that if you just want to get the output of a command, the backtick method on Kernel may be a better fit.

Write to stdin

ruby
process = ChildProcess.build("cat")

out      = Tempfile.new("duplex")
out.sync = true

process.io.stdout = process.io.stderr = out
process.duplex    = true # sets up pipe so process.io.stdin will be available after .start

process.start
process.io.stdin.puts "hello world"
process.io.stdin.close

process.poll_for_exit(exit_timeout_in_seconds)

out.rewind
out.read #=> "hello world\n"

Pipe output to another ChildProcess

ruby
search           = ChildProcess.build("grep", '-E', %w(redis memcached).join('|'))
search.duplex    = true # sets up pipe so search.io.stdin will be available after .start
search.io.stdout = $stdout
search.start

listing           = ChildProcess.build("ps", "aux")
listing.io.stdout = search.io.stdin
listing.start
listing.wait

search.io.stdin.close
search.wait

Ensure entire process tree dies

By default, the child process does not create a new process group. This means there's no guarantee that the entire process tree will die when the child process is killed. To solve this:

ruby
process = ChildProcess.build(*args)
process.leader = true
process.start

Detach from parent

ruby
process = ChildProcess.build("sleep", "10")
process.detach = true
process.start

Invoking a shell

As opposed to Kernel#system, Kernel#exec et al., ChildProcess will not automatically execute your command in a shell (like /bin/sh or cmd.exe) depending on the arguments. This means that if you try to execute e.g. gem executables (like bundle or gem) or Windows executables (with .com or .bat extensions) you may see a ChildProcess::LaunchError. You can work around this by being explicit about what interpreter to invoke:

ruby
ChildProcess.build("cmd.exe", "/c", "bundle")
ChildProcess.build("ruby", "-S", "bundle")

Log to file

Errors and debugging information are logged to $stderr by default but a custom logger can be used instead.

ruby
logger = Logger.new('logfile.log')
logger.level = Logger::DEBUG
ChildProcess.logger = logger

Caveats

  • With JRuby on Unix, modifying ENV["PATH"] before using childprocess could lead to 'Command not found' errors, since JRuby is unable to modify the environment used for PATH searches in java.lang.ProcessBuilder. This can be avoided by setting ChildProcess.posix_spawn = true.
  • With JRuby on Java >= 9, the JVM may need to be configured to allow JRuby to access neccessary implementations; this can be done by adding --add-opens java.base/java.io=org.jruby.dist and --add-opens java.base/sun.nio.ch=org.jruby.dist to the JAVA_OPTS environment variable that is used by JRuby when launching the JVM.

Implementation

ChildProcess 5+ uses Process.spawn from the Ruby core library for maximum portability.

Note on Patches/Pull Requests

  1. Fork it
  2. Create your feature branch (off of the development branch) git checkout -b my-new-feature dev
  3. Commit your changes git commit -am 'Add some feature'
  4. Push to the branch git push origin my-new-feature
  5. Create new Pull Request

Publishing a New Release

When publishing a new gem release:

  1. Ensure latest build is green on the dev branch
  2. Ensure CHANGELOG is updated
  3. Ensure version is bumped following Semantic Versioning
  4. Merge the dev branch into master: git checkout master && git merge dev
  5. Ensure latest build is green on the master branch
  6. Build gem from the green master branch: git checkout master && gem build childprocess.gemspec
  7. Push gem to RubyGems: gem push childprocess-.gem
  8. Tag commit with version, annotated with release notes: git tag -a

Copyright

Copyright (c) 2010-2015 Jari Bakken. See LICENSE for details.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言