Module: ProcessExecuter

Defined in:
lib/process_executer.rb,
lib/process_executer/status.rb,
lib/process_executer/options.rb,
lib/process_executer/version.rb,
lib/process_executer/monitored_pipe.rb

Overview

Execute a command in a subprocess and optionally capture its output

Defined Under Namespace

Classes: MonitoredPipe, Options, Status

Constant Summary collapse

VERSION =

The current Gem version

'1.1.0'

Class Method Summary collapse

Class Method Details

.spawn(*command, **options_hash) ⇒ Process::Status

Execute the specified command as a subprocess and return the exit status

This is a convenience method that calls Process.spawn and blocks until the command has terminated.

The command will be send the SIGKILL signal if it does not terminate within the specified timeout.

Examples:

status = ProcessExecuter.spawn('echo hello')
status.exited? # => true
status.success? # => true

with a timeout

status = ProcessExecuter.spawn('sleep 10', timeout: 0.01)
status.exited? # => false
status.success? # => nil
status.signaled? # => true
status.termsig # => 9

capturing stdout to a string

stdout = StringIO.new
status = ProcessExecuter.spawn('echo hello', out: stdout)
stdout.string # => "hello"

Parameters:

  • command (Array<String>)

    the command to execute

  • options_hash (Hash)

    the options to use when exectuting the command

Returns:

  • (Process::Status)

    the exit status of the proceess

See Also:



50
51
52
53
54
# File 'lib/process_executer.rb', line 50

def self.spawn(*command, **options_hash)
  options = ProcessExecuter::Options.new(**options_hash)
  pid = Process.spawn(*command, **options.spawn_options)
  wait_for_process(pid, options)
end