# Systemu-1.0.0

**URL:** <https://www.ruby-forum.com/t/systemu-1-0-0/74888>\
**Category:** Ruby\
**Created:** [November 7, 2006, 10:59am UTC](https://www.ruby-forum.com/t/systemu-1-0-0/74888 "2006-11-07T10:59:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [November 7, 2006, 10:59am UTC](https://www.ruby-forum.com/t/systemu-1-0-0/74888/1 "2006-11-07T10:59:54Z")

</div>

NAME

systemu.rb

SYNOPSIS

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

URIS

[http://rubyforge.org/projects/codeforpeople/](http://rubyforge.org/projects/codeforpeople/)  
[http://codeforpeople.com/lib/ruby/](http://codeforpeople.com/lib/ruby/)

INSTALL

gem install systemu

SAMPLES

\<========\< samples/a.rb \>========\>

~ \> cat samples/a.rb

```
 #
 # systemu can be used on any platform to return status, stdout, and 

```

stderr of  
# any command. unlike other methods like open3/popen4 there is  
zero danger of  
# full pipes or threading issues hanging your process or  
subprocess.  
#  
require ‘systemu’

```
   date = %q( ruby -e" t = Time.now; STDOUT.puts t; STDERR.puts t 

```

" )

```
   status, stdout, stderr = systemu date
   p [status, stdout, stderr]

```

~ \> ruby samples/a.rb

```
 [#<Process::Status: pid=9960,exited(0)>, "Fri Nov 03 17:22:23 MST 

```

2006\n", “Fri Nov 03 17:22:23 MST 2006\n”]

\<========\< samples/b.rb \>========\>

~ \> cat samples/b.rb

```
 #
 # quite a few keys can be passed to the command to alter it's 

```

behaviour. if  
# either stdout or stderr is supplied those objects should  
respond\_to? ‘\<\<’  
# and only status will be returned  
#  
require ‘systemu’

```
   date = %q( ruby -e" t = Time.now; STDOUT.puts t; STDERR.puts t 

```

" )

```
   stdout, stderr = '', ''
   status = systemu date, 'stdout' => stdout, 'stderr' => stderr
   p [status, stdout, stderr]

```

~ \> ruby samples/b.rb

```
 [#<Process::Status: pid=9965,exited(0)>, "Fri Nov 03 17:22:23 MST 

```

2006\n", “Fri Nov 03 17:22:23 MST 2006\n”]

\<========\< samples/c.rb \>========\>

~ \> cat samples/c.rb

```
 #
 # of course stdin can be supplied too. synonyms for 'stdin' 

```

include ‘0’ and  
# 0. the other stdio streams have similar shortcuts  
#  
require ‘systemu’

```
   cat = %q( ruby -e" ARGF.each{|line| puts line} " )

   status = systemu cat, 0=>'the stdin for cat', 1=>stdout=''
   puts stdout

```

~ \> ruby samples/c.rb

```
 the stdin for cat

```

\<========\< samples/d.rb \>========\>

~ \> cat samples/d.rb

```
 #
 # the cwd can be supplied
 #
   require 'systemu'
   require 'tmpdir'

   pwd = %q( ruby -e" STDERR.puts Dir.pwd " )

   status = systemu pwd, 2=>(stderr=''), :cwd=>Dir.tmpdir
   puts stderr

```

~ \> ruby samples/d.rb

```
 /tmp

```

\<========\< samples/e.rb \>========\>

~ \> cat samples/e.rb

```
 #
 # any environment vars specified are merged into the child's 

```

environment  
#  
require ‘systemu’

```
   env = %q( ruby -r yaml -e" puts ENV['answer'] " )

   status = systemu env, 1=>stdout='', 'env'=>{ 'answer' => 0b101010 

```

}  
puts stdout

~ \> ruby samples/e.rb

```
 42

```

\<========\< samples/f.rb \>========\>

~ \> cat samples/f.rb

```
 #
 # if a block is specified then it is passed the child pid and run 

```

in a  
# background thread. note that this thread will **not** be blocked  
during the  
# execution of the command so it may do useful work such as killing  
the child  
# if execution time passes a certain threshold  
#  
require ‘systemu’

```
   looper = %q( ruby -e" loop{ STDERR.puts Time.now.to_i; sleep 1 } 

```

" )

```
   status, stdout, stderr =
     systemu looper do |cid|
       sleep 3
       Process.kill 9, cid
     end

   p [status, stdout, stderr]

```

~ \> ruby samples/f.rb

```
 [#<Process::Status: pid=9985,signaled(SIGKILL=9)>, "", 

```

“1162599744\n1162599745\n1162599746\n1162599747\n”]

enjoy.

-a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [November 7, 2006, 11:01am UTC](https://www.ruby-forum.com/t/systemu-1-0-0/74888/2 "2006-11-07T11:01:59Z")

</div>

On 11/4/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> NAME

Looks great Ara as always.  
I am using Popen7 very often (Linux only) it is the heart of many a  
webrick  
code to administer my Linux Boxes via my Browser, I never had _any_  
problems. (little data in the pipes for sure \<\< 1MB)  
But I’ll definitely switch to this one looks so concise.

Thx for your efforts.

Robert

–  
The reasonable man adapts himself to the world; the unreasonable one  
persists in trying to adapt the world to himself. Therefore all progress  
depends on the unreasonable man.

- George Bernard Shaw

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [November 7, 2006, 11:03am UTC](https://www.ruby-forum.com/t/systemu-1-0-0/74888/3 "2006-11-07T11:03:00Z")

</div>

On Sun, 5 Nov 2006, Robert D. wrote:

> problems. (little data in the pipes for sure \<\< 1MB)  
> But I’ll definitely switch to this one looks so concise.
> 
> Thx for your efforts.
> 
> Robert

ooops!

i should have mentioned in the first message, but there’s no need to  
switch  
from open4 if you’re on a posix operating system - the main reason for  
this  
package is people wanting to write open4 style code that will be  
portable on  
windows, or windows users who need open4 semantics. basically, afaik,  
the  
popen4 module for windows has a fatal flaw which can/will cause hangs in  
some  
situations. open4, on \*nix, will never cause hangs and systemu on  
windows or  
nix shouldn’t either.

summary: if you are on windows and need process control with pid,  
stderr, and  
stdout - or if you are trying to write unix/windows code that requires  
those  
things this is for you. otherwise i’d highly reccomend sticking with  
open4,  
the performance is better and the spawn method gives you everything  
systemu  
does and more with less overhead.

sorry for any confustion - hopefully this has made it clear.

kind regards.

-a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [November 7, 2006, 11:03am UTC](https://www.ruby-forum.com/t/systemu-1-0-0/74888/4 "2006-11-07T11:03:04Z")

</div>

removed\_email\_address@domain.invalid wrote:

> ```
> # quite a few keys can be passed to the command to alter it's behaviour. if
> # either stdout or stderr is supplied those objects should respond_to? '<<'
> # and only status will be returned
> 
> ```

[snip]

> ```
> # of course stdin can be supplied too. synonyms for 'stdin' include '0' and
> # 0. the other stdio streams have similar shortcuts
> 
> ```

[snip]

> ```
> # the cwd can be supplied
> 
> ```

[snip]

> ```
> # any environment vars specified are merged into the child's environment
> 
> ```

[snip]

> ```
> # if a block is specified then it is passed the child pid and run in a
> # background thread. note that this thread will __not__ be blocked during the
> # execution of the command so it may do useful work such as killing the child
> # if execution time passes a certain threshold
> 
> ```

[snip]

There’s a scene in Total Recall where the cabbie says to the prostitute  
with three breasts:  
“Damn, baby, you make me wish I had three hands!”

You, Ara, make me wish that I had a burning need to frequently capture  
stdout and stderr from system processes on Windows. Preferably while  
specifying extra environment variables.

Looks nice. 🙂

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [November 7, 2006, 11:03am UTC](https://www.ruby-forum.com/t/systemu-1-0-0/74888/5 "2006-11-07T11:03:18Z")

</div>

On 11/5/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Looks great Ara as always.  
> > ooops!  
> > situations. open4, on \*nix, will never cause hangs and systemu on windows  
> > systemu  
> > Good to know, thx.  
> > Robert

–  
The reasonable man adapts himself to the world; the unreasonable one  
persists in trying to adapt the world to himself. Therefore all progress  
depends on the unreasonable man.

- George Bernard Shaw
