Class: Subexec

### Home Classes Methods

### In Files

- lib/subexec.rb

### Parent

Object

### Methods

- ::new
- ::run
- #run!

### Class/Module Index

Quicksearch

- Subexec

No matching classes.

# Subexec

- by Peter Kieltyka

- github/nulayer/subexec

## Description

Subexec is a simple library that spawns an external command with an optional timeout parameter. It relies on Ruby 1.9's Process.spawn method. Also, it works with synchronous and asynchronous code.

Useful for libraries that are Ruby wrappers for CLI's. For example, resizing images with ImageMagick's mogrify command sometimes stalls and never returns control back to the original process. Subexec executes mogrify and preempts if it gets lost.

## Usage

# Print hello sub = Subexec.run "echo 'hello' && sleep 3", :timeout => 5 puts sub.output # returns: hello puts sub.exitstatus # returns: 0

# Timeout process after a second sub = Subexec.run "echo 'hello' && sleep 3", :timeout => 1 puts sub.output # returns: puts sub.exitstatus # returns:

### Constants

VERSION

### Attributes

command[RW]
exitstatus[RW]
lang[RW]
log_file[RW]
output[RW]
pid[RW]
timeout[RW]

### Public Class Methods

new(command, options={}) click to toggle source

    # File lib/subexec.rb, line 45
    def initialize(command, options={})
      self.command    = command
      self.lang       = options[:lang]      || "C"
      self.timeout    = options[:timeout]   || -1     # default is to never timeout
      self.log_file   = options[:log_file]
      self.exitstatus = 0
    end

run(command, options={}) click to toggle source

    # File lib/subexec.rb, line 39
    def self.run(command, options={})
      sub = new(command, options)
      sub.run!
      sub
    end

### Public Instance Methods

run!() click to toggle source

    # File lib/subexec.rb, line 53
    def run!
      if RUBY_VERSION >= '1.9' && RUBY_ENGINE != 'jruby'
        spawn
      else
        exec
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.