GithubHelp home page GithubHelp logo

anthrax3 / childprocess Goto Github PK

View Code? Open in Web Editor NEW

This project forked from busterb/childprocess

0.0 0.0 0.0 373 KB

Cross-platform Ruby library for managing child processes.

License: MIT License

Ruby 100.00%

childprocess's Introduction

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.

Build Status Gem Version Code Climate Coverage Status

Usage

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

Basic examples

process = ChildProcess.build("ruby", "-e", "sleep")

# inherit stdout/stderr from parent...
process.io.inherit!

# ...or pass an IO
process.io.stdout = Tempfile.new("child-output")

# modify the environment for the child
process.environment["a"] = "b"
process.environment["c"] = nil

# set the child's working directory
process.cwd = '/some/path'

# start the process
process.start

# check process status
process.alive?    #=> true
process.exited?   #=> false

# wait indefinitely for process to exit...
process.wait
process.exited?   #=> true

# get the exit code
process.exit_code #=> 0

# ...or poll for exit + force quit
begin
  process.poll_for_exit(10)
rescue ChildProcess::TimeoutError
  process.stop # tries increasingly harsher methods to kill the process.
end

Advanced examples

Output to pipe

r, w = IO.pipe

proc = ChildProcess.build("echo", "foo")
proc.io.stdout = proc.io.stderr = w
proc.start
w.close

begin
  loop { print r.readpartial(8192) }
rescue EOFError
end

proc.wait

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

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

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

Prefer posix_spawn on *nix

If the parent process is using a lot of memory, fork+exec can be very expensive. The posix_spawn() API removes this overhead.

ChildProcess.posix_spawn = true
process = ChildProcess.build(*args)

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:

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

Detach from parent

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:

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

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 environemnt used for PATH searches in java.lang.ProcessBuilder. This can be avoided by setting ChildProcess.posix_spawn = true.

Implementation

How the process is launched and killed depends on the platform:

  • Unix : fork + exec (or posix_spawn if enabled)
  • Windows : CreateProcess() and friends
  • JRuby : java.lang.{Process,ProcessBuilder}

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Copyright

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

childprocess's People

Contributors

antoinelyset avatar benlangfeld avatar fletcherm avatar iain avatar iconoclast avatar jarib avatar jarl-dk avatar jgraichen avatar jonrowe avatar kbarrette avatar limhoff-r7 avatar mitchellh avatar msassak avatar nicksieger avatar phiggins avatar portertech avatar robertwahler avatar salimane avatar schmich avatar sheerun avatar stefanor avatar thedjinn avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.