• Stars
    star
    194
  • Rank 200,219 (Top 4 %)
  • Language
    Ruby
  • License
    Other
  • Created over 15 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

open child process with handles on pid, stdin, stdout, and stderr: manage child processes and their io handles easily.
URIS

  http://rubyforge.org/projects/codeforpeople/
  http://www.codeforpeople.com/lib/ruby/

SYNOPSIS

  open child process with handles on pid, stdin, stdout, and stderr: manage
  child processes and their io handles easily.

INSTALL

  ~> gem install open4

SAMPLES

  ----------------------------------------------------------------------------
  simple usage
  ----------------------------------------------------------------------------

    harp: > cat sample/simple.rb
    require "open4"

    pid, stdin, stdout, stderr = Open4::popen4 "sh"

    stdin.puts "echo 42.out"
    stdin.puts "echo 42.err 1>&2"
    stdin.close

    ignored, status = Process::waitpid2 pid

    puts "pid        : #{ pid }"
    puts "stdout     : #{ stdout.read.strip }"
    puts "stderr     : #{ stderr.read.strip }"
    puts "status     : #{ status.inspect }"
    puts "exitstatus : #{ status.exitstatus }"


    harp: > ruby sample/simple.rb
    pid        : 17273
    stdout     : 42.out
    stderr     : 42.err
    status     : #<Process::Status: pid=17273,exited(0)>
    exitstatus : 0


  ----------------------------------------------------------------------------
  in block form - the child process is automatically waited for
  ----------------------------------------------------------------------------

    harp: > cat sample/block.rb
    require 'open4'

    status =
      Open4::popen4("sh") do |pid, stdin, stdout, stderr|
        stdin.puts "echo 42.out"
        stdin.puts "echo 42.err 1>&2"
        stdin.close

        puts "pid        : #{ pid }"
        puts "stdout     : #{ stdout.read.strip }"
        puts "stderr     : #{ stderr.read.strip }"
      end

        puts "status     : #{ status.inspect }"
        puts "exitstatus : #{ status.exitstatus }"


    harp: > ruby sample/block.rb
    pid        : 17295
    stdout     : 42.out
    stderr     : 42.err
    status     : #<Process::Status: pid=17295,exited(0)>
    exitstatus : 0

  ----------------------------------------------------------------------------
  exceptions are marshaled from child to parent if fork/exec fails
  ----------------------------------------------------------------------------

    harp: > cat sample/exception.rb
    require "open4"
    Open4::popen4 "noexist"


    harp: > ruby sample/exception.rb
    /dmsp/reference/ruby-1.8.1//lib/ruby/site_ruby/open4.rbπŸ’―in `popen4': No such file or directory - noexist (Errno::ENOENT)
            from sample/exception.rb:3

  ----------------------------------------------------------------------------
  the spawn method provides and even more convenient method of running a
  process, allowing any object that supports 'each', 'read', or 'to_s' to be
  given as stdin and any objects that support '<<' to be given as
  stdout/stderr.  an exception is thrown if the exec'd cmd fails (nonzero
  exitstatus) unless the option 'raise'=>false is given
  ----------------------------------------------------------------------------
  
    harp: > cat sample/spawn.rb
    require 'open4'
    include Open4

    cat = '  ruby -e"  ARGF.each{|line| STDOUT << line}  "  '

    stdout, stderr = '', ''
    status = spawn cat, 'stdin' => '42', 'stdout' => stdout, 'stderr' => stderr
    p status
    p stdout
    p stderr

    stdout, stderr = '', ''
    status = spawn cat, 0=>'42', 1=>stdout, 2=>stderr
    p status
    p stdout
    p stderr


    harp: > RUBYLIB=lib ruby sample/spawn.rb
    0
    "42"
    ""
    0
    "42"
    ""


  ----------------------------------------------------------------------------
  the bg/background method is similar to spawn, but the process is
  automatically set running in a thread.  the returned thread has several
  methods added dynamically which return the pid and blocking calls to the
  exitstatus.
  ----------------------------------------------------------------------------

    harp: > cat sample/bg.rb
    require 'yaml'
    require 'open4'
    include Open4

    stdin = '42'
    stdout = ''
    stderr = ''

    t = bg 'ruby -e"sleep 4; puts ARGF.read"', 0=>stdin, 1=>stdout, 2=>stderr

    waiter = Thread.new{ y t.pid => t.exitstatus } # t.exitstatus is a blocking call!

    while((status = t.status))
      y "status" => status
      sleep 1
    end

    waiter.join

    y "stdout" => stdout


    harp: > ruby sample/bg.rb
    ---
    status: run
    ---
    status: sleep
    ---
    status: sleep
    ---
    status: sleep
    ---
    21357: 0
    ---
    stdout: "42\n"

  ----------------------------------------------------------------------------
  the timeout methods can be used to ensure execution is preceding at the
  desired interval.  note also how to setup a 'pipeline'
  ----------------------------------------------------------------------------

    harp: > cat sample/stdin_timeout.rb
    require 'open4'

    producer = 'ruby -e" STDOUT.sync = true; loop{sleep(rand+rand) and puts 42} "'

    consumer = 'ruby -e" STDOUT.sync = true; STDIN.each{|line| puts line} "'

    open4(producer) do |pid, i, o, e|

      open4.spawn consumer, :stdin=>o, :stdout=>STDOUT, :stdin_timeout => 1.4

    end


    harp: > ruby sample/stdin_timeout.rb
    42
    42
    42
    42
    42
    /dmsp/reference/ruby-1.8.1//lib/ruby/1.8/timeout.rb:42:in `relay': execution expired (Timeout::Error)

  ----------------------------------------------------------------------------
  pfork4 is similar to popen4, but instead of executing a command, it runs
  ruby code in a child process. if the child process raises an exception, it
  propagates to the parent.
  ----------------------------------------------------------------------------

    harp: > cat sample/pfork4.rb
    require 'open4'

    echo = lambda do
      $stdout.write $stdin.read
      raise 'finish implementing me'
    end

    org_message = "hello, world!"
    got_message = nil
    exception   = nil

    begin
      Open4.pfork4(echo) do |cid, stdin, stdout, stderr|
        stdin.write org_message
        stdin.close
        got_message = stdout.read
      end
    rescue RuntimeError => e
      exception = e.to_s
    end

    puts "org_message: #{org_message}"
    puts "got_message: #{got_message}"
    puts "exception  : #{exception}"


    harp: > ruby sample/pfork4.rb
    org_message: hello, world!
    got_message: hello, world!
    exception  : finish implementing me

HISTORY
  1.0.0
    - added ability for spawn to take a proc (respond_to?(:call))

      cmd = ' ruby -e" 42.times{ puts 0b101010 } " '
      include Open4
      spawn cmd, :stdout => lambda{|buf| puts buf} 


  0.9.5:
    - another patch from Corey Jewett, this time dealing with ruby's handling
      of chdir and threads.  basically the 'cwd' keyword to open4 cannot work
      with multiple threads (aka background) because ruby cannot cause green
      threads to have an actuall different working dir.  the moral is that the
      :cwd/'cwd' keyword to spawn will work with 0 or 1 threads in effect.
  
  0.9.4:
    - patch to #background from Corey Jewett


  0.9.3:
    - removed some debugging output accidentally left in 0.9.2.  arggh!

  0.9.2:
    - fixed a descriptor leak.  thanks Andre Nathan.

  0.9.1:
    - fixed warning with '-w' : @cid not initialized.  thanks blaise tarr.

  0.9.0:
    - added the ability for open4.spawn to take either an array of arguments
      or multiple arguments in order to specify the argv for the command run.
      for example

        open4.spawn ['touch', 'difficult to "quote"'], :stdout=>STDOUT

      same thing

        open4.spawn 'touch', 'difficult to "quote"', :stdout=>STDOUT

      thanks to jordan breeding for this suggestion


    - added 'cwd'/:cwd keyword.  usage is pretty obivous

        open4.spawn 'pwd', 1=>STDOUT, :cwd=>'/tmp'   #=> /tmp

      this one also from jordan

  0.8.0:

    - fixed a critical bug whereby a process producing tons of stdout, but for
      which the stdout was not handled, would cause the child process to
      become blocked/hung writing to the pipe.  eg, this command would cause a
      hang

        include Open4

        spawn 'ruby -e"  puts Array.new(65536){ 42 }  "'

      whereas this one would not

        include Open4

        spawn 'ruby -e"  puts Array.new(65536){ 42 }  "', :stdout=>StringIO.new

      this version handles the former by spawning a 'null' thread which reads,
      but does not process stdout/stderr.  that way commands which generate
      tons of output will never become blocked.

  0.7.0:
    - merged functionality of exitstatus/status keywords:

        include Open4

        spawn 'ruby -e "exit 42"'                 # raises 
        spawn 'ruby -e "exit 42"', :status=>true  # ok, returns status
        spawn 'ruby -e "exit 42"', :status=>42    # raises if status != 42
        spawn 'ruby -e "exit 42"', :status=>0,42  # raises if status != 0||42

    - the 0.6.0 was broken on rubyforge... this release fixes that (somehow!?)

  0.6.0:
    - added feature for exitstatus to be list of acceptable exit statuses

        Open4.spawn 'ruby -e "exit 42"'                      # raises
        Open4.spawn 'ruby -e "exit 42"', :exitstatus=>[0,42] # ok

    - added :status switch, which will always simply return the status (no
      error thrown for failure)

        Open4.spawn 'ruby -e "exit 42"'                          # raises 
        status = Open4.spawn 'ruby -e "exit 42"', :status=>true  # ok 

      note, however, that any SpawnError does in fact contain the failed
      status so, even when they are thrown, error status can be retrieved:

        include Open4

        status =
          begin
            spawn 'ruby -e "exit 42"'
          rescue SpawnError => e
            warn{ e }
            e.status
          end

  0.5.1:
    - fixes a __critical__ but in ThreadEnsemble class that had a race
      condition that could cause thread deadlock.  sorry bout that folks.

  0.5.0:
    - on the suggestion of tim pease (thanks tim!), i added timeout features
      to open4.  the command run may have an overall timeout and individual
      timeouts set for each of the io handles.  for example

        cmd = 'command_that_produce_out_at_one_second_intervals'

        open4.spawn cmd, :stdout_timeout => 2 

      or 

        cmd = 'command_that_should_complete_in_about_one_minute'

        open4.spawn cmd, :timeout => 60

      or

        cmd = 'consumes_input_at_one_line_per_second_rate'

        input = %w( 42 forty-two 42.0 )

        open4.spawn cmd, :stdin=>input, :stdin_timeout=>1

    - added 'open4' alias so one can write

        open4.spawn  vs Open4.spawn

      or even

        open4(cmd) do |pid,i,o,e|
        end

    - added signal info to SpawnError

  0.4.0:
    - improved error handling contributed by jordan breeding.
    - introduction of background/bg method

  0.3.0 :
    - bug fix from jordan breeding.  general clean up.  added spawn method.

  0.2.0 :
    - added exception marshaled from child -> parent when exec fails.  thanks
      to jordan breeding for a patch (yay!) and paul brannan for this most
      excellent idea.

  0.1.0 :
    - fixed docs to correctly show return value of popen4 (pid first not last).
      thanks Stefanie Tellex <[email protected]> for catching this. 
  0.0.0 :
    - initial version

AUTHOR

  [email protected]

LICENSE

  ruby's

More Repositories

1

sekrets

sekrets is a command line tool and library used to securely manage encrypted files and settings in your rails' applications and git repositories.
Ruby
268
star
2

main

a class factory and dsl for generating command line programs real quick
Ruby
266
star
3

map

the ruby container you've always wanted: an ordered string/symbol indifferent hash
Ruby
167
star
4

systemu

univeral capture of stdout and stderr and handling of child process pid for windows, *nix, etc.
Ruby
126
star
5

testy

a BDD testing framework for ruby that's mad at the world and plans to kick it's ass in 78 freakin lines of code
Ruby
98
star
6

configuration

pure ruby scoped configuration files.
Ruby
80
star
7

forkoff

brain-dead simple parallel processing for ruby
Ruby
73
star
8

fattr

fattr.rb is a "fatter attr" for ruby and borrows heavily from the metakoans.rb ruby quiz
Ruby
73
star
9

session

session offers a persistent way to drive the shell (/bin/sh) via ruby
Ruby
63
star
10

dao

sa-weet data access object library for rails. top secret.
Ruby
61
star
11

lockfile

a ruby library for creating NFS safe lockfiles
Ruby
54
star
12

macaddr

cross platform mac address determination for ruby
Ruby
47
star
13

middleman-gibberish

password protect middleman pages - even on s3
JavaScript
47
star
14

threadify

threadify.rb makes it stupid easy to process a bunch of data using 'n' worker threads
Ruby
39
star
15

shared

shared.rb provides a super easy way to share code between classes or modules in a simple way.
Ruby
33
star
16

bj

Backgroundjob (Bj) is a brain dead simple, zero admin, background priority queue for Rails.
Ruby
31
star
17

tagz

tagz.rb generates html, xml, or any sgml variant like a small ninja running across the backs of a herd of giraffes swatting of heads like a mark-up weedwacker.
Ruby
31
star
18

demon

demon.rb - the ruby daemon library you've been waiting for
Ruby
29
star
19

slave

easy management of child process works over pipes and drb
Ruby
27
star
20

rails_build

A very small, very simple, very fast, and bullet proof static site generator built as a Rails 5 engine.
Ruby
25
star
21

objectpool

a simple, robust, generic thread-safe object pool for ruby
Ruby
20
star
22

rq

ruby queue is a zero-admin zero-configuration tool used to create instant unix clusters
Ruby
20
star
23

raptcha

low drain bamage, storage-less, session-less, plugin-less, zero admin, single-source-file secure captcha system for ruby and/or rails.
Ruby
19
star
24

arrayfields

allow keyword access to array instances.
Ruby
18
star
25

tumblr

a command line utility and library for the excellent tumblr blogging platform
Ruby
18
star
26

helene

helene is a plugin for writing rails applications on top of amazon's aws platform including sdb, s3, and sqs
Ruby
17
star
27

rego

run arbitrary commands easily when files change
Ruby
16
star
28

fbomb

fbomb is the dangerous flowdock bot
Ruby
16
star
29

fucking_favicons

fucking favicons fucking suck
Ruby
15
star
30

mongoid-haystack

a mongoid 3 zero-config, zero-integration, POLS pure mongo fulltext solution
Ruby
14
star
31

coxswain

encapsulate pre-forking master / worker pattern for ruby
Ruby
12
star
32

jquery.bires

bandwidth limited progressive image enhancement
JavaScript
12
star
33

hashish

awesome data access layer for rails/ruby projects
Ruby
12
star
34

rails_current

track current_STUFF mo betta
Ruby
9
star
35

assassin

no zombies ever, not even on `exit!` or `kill -9`
Ruby
9
star
36

default_url_options

all relative urls in rails all the time. even in mailers.
Ruby
8
star
37

forkhandle

a teeny library / design pattern for managing connections in a process and thread safe fashion
Ruby
7
star
38

rememberthemilk

simple (162 loc), json only, interface to the excellent RememberTheMilk API
Ruby
7
star
39

wrap

non-sucky :before and :after callbacks for any ruby class
Ruby
7
star
40

conducer

a model+view component for rails that combines the conductor and presenter pattern
Ruby
7
star
41

ro

ro is library for managing your site's content in git, as god intended.
Ruby
7
star
42

rails_default_url_options

you really can have default_url_options everywhere. even in mailers.
Ruby
7
star
43

options

options.rb handles keyword options in ruby in a simple and robust way
Ruby
6
star
44

irbcp

irbcp gives access to your system's clipboard (copy and paste) from irb
Ruby
6
star
45

rails_errors2html

simple and sane active_model error html rendering
Ruby
6
star
46

fukung

perhaps the most important ruby code EVAAARRR! gets random images from http://fukung.net.
Ruby
6
star
47

rails_nav

objectified navigation for rails
Ruby
5
star
48

cssjs

a zero learning curve zero contraints dsl for writing css stylesheets in javascript
JavaScript
5
star
49

pork

pork supports parallel programming in ruby using forked actors and durable sqlite message queues
Ruby
5
star
50

isolation

a small rails app to demonstrate what *you* don't understand about RDBMS transactions
Ruby
5
star
51

mongoid-fts

enable mongodb's new fulltext simply and quickly on your mongoid models, including pagination.
Ruby
4
star
52

lru_cache

a simple but efficient implementation of a size limited least recently used cache in ruby
Ruby
4
star
53

terminator

an external timeout mechanism based on processes and signals
Ruby
4
star
54

cdc

uber simple cross domain communication for javascript/iframes
JavaScript
4
star
55

coerce

a ruby library full of common cast/coercion operations
Ruby
4
star
56

rails_view

render views from anywhere. even without a controller context
Ruby
4
star
57

rails_helper

helper = Helper.new and helper.link_to(:foo)
Ruby
4
star
58

ggeocode

simple wrapper on google's new geocoding api
Ruby
4
star
59

suck

gem to show issues with gem/minigem load ordering
Shell
3
star
60

alpo

a library and design pattern for building sane web applications on top of the rails' stack
Ruby
3
star
61

backup.rake

rails' rake task for backup up and loading data+assets as yaml+files
Ruby
3
star
62

id3rename

id3rename is a program to do simple renaming of mp3 files
3
star
63

nfsutils

Ruby FileUtils for NFS
3
star
64

bookify

development moved to https://github.com/everlater/bookify
Ruby
3
star
65

senv

the 12-factor environment tool your mother told you to use
Ruby
3
star
66

codeforpeople

billions and billions of libs
3
star
67

ledis

a K.I.S.S auto-rotating redis logger for ruby/rails
Ruby
3
star
68

kgb

ultra lightweight javascript decision tree builder
JavaScript
3
star
69

image_cache

a small utility library to facility caching image uploads between form validation failures.
2
star
70

gnip-ruby

Ruby library for utilizing Gnip services.
Ruby
2
star
71

linked_list

a simple linked list implementation for ruby
Ruby
2
star
72

bucket

bucket is a command-line interface for amazon's s3 service
2
star
73

tmpdir_block

extends ruby's built-in Dir.tmpdir to accept a block.
Ruby
2
star
74

bestofyoutube

simple ruby library to grab some good video urls from http://bestofyoutube.com
Ruby
2
star
75

superhash

A general mechanism for defining attribute inheritance structures among objects of any type, not just classes
Ruby
2
star
76

testing.rb

adds the minimal features required in order to make test/unit not suck
Ruby
2
star
77

openobject

a simple property based container that's much more capable than a blankslate but far less polluted than ruby's built-in OpenStruct
Ruby
2
star
78

candy_store

hybrid session store that combines rails' built-in cookie based session store with its database backed one
Ruby
2
star
79

slug

a simple slug library. unicode prepared.
Ruby
2
star
80

markdown

my markdown script
Ruby
2
star
81

mongoid-bolt

mongoid-bolt is a concrete lock implementation and mixin.
Ruby
2
star
82

growltdf

growltdf is the greatest program evar. it let's you scrape http://cyclingnews.com for TDF updates and dump them into growl to you can keep up with the race while working.
2
star
83

nmap

narray + mmap = wicked fast persistent numerical arrays for ruby
Ruby
2
star
84

mob

background jobs for mongoid
Ruby
1
star
85

foobar

1
star
86

ydb

mo-betta the yaml/store.
Ruby
1
star
87

hrs

tracking teh hours from the cli
Ruby
1
star
88

mp3scrape

download shit-loads of mp3s from web pages
Ruby
1
star
89

wapp

golden.image
Ruby
1
star
90

upload_cache

a small utility library to facility caching http file uploads between form validation failures. designed for rails, but usable anywhere.
Ruby
1
star
91

forkify

forkify.rb makes it easy to process a bunch of data using 'n' worker processes
Ruby
1
star
92

ansible

magic unicorns for your deployz
Ruby
1
star
93

campfire

a command line script for using 37signal's campfire chat room
1
star
94

cast

a collection of casting methods for ruby
1
star
95

fifo

a very simple javascript fifo queue / cache
JavaScript
1
star
96

ey-cloud-recipes

A starter repo for custom chef recipes on EY's cloud platform
Ruby
1
star
97

test

1
star
98

one-click-hugo-cms

CSS
1
star
99

attributes

the implementation of attributes.rb borrows many of the best ideas from the metakoans.rb ruby quiz (ps. fattr >= attributes)
Ruby
1
star
100

gnip-expander

relay a gnip publisher stream to another, expanding shortened uris in the process
Ruby
1
star