# \[ANN\] systemu-1.2.0

**URL:** <https://rubytalk.org/t/ann-systemu-1-2-0/42812>\
**Category:** ruby-talk\
**Created:** [6 December 2007 23:01 UTC](https://rubytalk.org/t/ann-systemu-1-2-0/42812 "2007-12-06T23:01:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [6 December 2007 23:01 UTC](https://rubytalk.org/t/ann-systemu-1-2-0/42812/1 "2007-12-06T23:01:44Z")

</div>

NAME

&nbsp;&nbsp;&nbsp;systemu

SYNOPSIS

&nbsp;&nbsp;&nbsp;univeral capture of stdout and stderr and handling of child process pid for windows, \*nix, etc.

URIS

&nbsp;&nbsp;&nbsp;[http://rubyforge.org/projects/codeforpeople/](http://rubyforge.org/projects/codeforpeople/)  
&nbsp;&nbsp;&nbsp;[http://codeforpeople.com/lib/ruby/](http://codeforpeople.com/lib/ruby/)  
&nbsp;&nbsp;&nbsp;[http://codeforpeople.rubyforge.org/svn/](http://codeforpeople.rubyforge.org/svn/)

INSTALL

&nbsp;&nbsp;&nbsp;gem install systemu

HISTORY

&nbsp;&nbsp;&nbsp;1.2.0

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- fixed handling of background thread management - needed  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.current.abort\_on\_exception = true

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- fixed reporting of child pid, it was reported as the parent's pid before

SAMPLES

&nbsp;&nbsp;&nbsp;\<========\< samples/a.rb \>========\>

&nbsp;&nbsp;&nbsp;~ \> cat samples/a.rb

> **···**
>
> #  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# systemu can be used on any platform to return status, stdout, and stderr of  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# any command. unlike other methods like open3/popen4 there is zero danger of  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# full pipes or threading issues hanging your process or subprocess.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'systemu'
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date = %q( ruby -e" t = Time.now; STDOUT.puts t; STDERR.puts t " )
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;status, stdout, stderr = systemu date  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p [status, stdout, stderr]
> 
> &nbsp;&nbsp;&nbsp;~ \> ruby samples/a.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[#\<Process::Status: pid=987,exited(0)\>, "Thu Dec 06 16:01:59 -0700 2007\n", "Thu Dec 06 16:01:59 -0700 2007\n"]
> 
> &nbsp;&nbsp;&nbsp;\<========\< samples/b.rb \>========\>
> 
> &nbsp;&nbsp;&nbsp;~ \> cat samples/b.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# quite a few keys can be passed to the command to alter it's behaviour. if  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# either stdout or stderr is supplied those objects should respond\_to? '\<\<'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# and only status will be returned  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'systemu'
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date = %q( ruby -e" t = Time.now; STDOUT.puts t; STDERR.puts t " )
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stdout, stderr = '', ''  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;status = systemu date, 'stdout' =\> stdout, 'stderr' =\> stderr  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p [status, stdout, stderr]
> 
> &nbsp;&nbsp;&nbsp;~ \> ruby samples/b.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[#\<Process::Status: pid=992,exited(0)\>, "Thu Dec 06 16:01:59 -0700 2007\n", "Thu Dec 06 16:01:59 -0700 2007\n"]
> 
> &nbsp;&nbsp;&nbsp;\<========\< samples/c.rb \>========\>
> 
> &nbsp;&nbsp;&nbsp;~ \> cat samples/c.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# of course stdin can be supplied too. synonyms for 'stdin' include '0' and  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# 0. the other stdio streams have similar shortcuts  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'systemu'
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cat = %q( ruby -e" ARGF.each{|line| puts line} " )
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;status = systemu cat, 0=\>'the stdin for cat', 1=\>stdout=''  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts stdout
> 
> &nbsp;&nbsp;&nbsp;~ \> ruby samples/c.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the stdin for cat
> 
> &nbsp;&nbsp;&nbsp;\<========\< samples/d.rb \>========\>
> 
> &nbsp;&nbsp;&nbsp;~ \> cat samples/d.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# the cwd can be supplied  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'systemu'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'tmpdir'
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pwd = %q( ruby -e" STDERR.puts Dir.pwd " )
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;status = systemu pwd, 2=\>(stderr=''), :cwd=\>Dir.tmpdir  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts stderr
> 
> &nbsp;&nbsp;&nbsp;~ \> ruby samples/d.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/private/tmp
> 
> &nbsp;&nbsp;&nbsp;\<========\< samples/e.rb \>========\>
> 
> &nbsp;&nbsp;&nbsp;~ \> cat samples/e.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# any environment vars specified are merged into the child's environment  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'systemu'
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;env = %q( ruby -r yaml -e" puts ENV['answer'] " )
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;status = systemu env, 1=\>stdout='', 'env'=\>{ 'answer' =\> 0b101010 }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts stdout
> 
> &nbsp;&nbsp;&nbsp;~ \> ruby samples/e.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;42
> 
> &nbsp;&nbsp;&nbsp;\<========\< samples/f.rb \>========\>
> 
> &nbsp;&nbsp;&nbsp;~ \> cat samples/f.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# if a block is specified then it is passed the child pid and run in a  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# background thread. note that this thread will \_\_not\_\_ be blocked during the  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# execution of the command so it may do useful work such as killing the child  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# if execution time passes a certain threshold  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'systemu'
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;looper = %q( ruby -e" loop{ STDERR.puts Time.now.to\_i; sleep 1 } " )
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;status, stdout, stderr =  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;systemu looper do |cid|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep 3  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Process.kill 9, cid  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p status  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p stderr
> 
> &nbsp;&nbsp;&nbsp;~ \> ruby samples/f.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#\<Process::Status: pid=1012,signaled(SIGKILL=9)\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"1196982119\n1196982120\n1196982121\n"
> 
> enjoy.
> 
> a @ [http://codeforpeople.com/](http://codeforpeople.com/)  
> --  
> it is not enough to be compassionate. you must act.  
> h.h. the 14th dalai lama
