Class: Foreman::Process

### Home Classes Methods

### In Files

- lib/foreman/process.rb

### Parent

Object

### Methods

- ::new
- #alive?
- #cwd
- #dead?
- #exec
- #expanded_command
- #kill
- #run

### Class/Module Index

Quicksearch

- Foreman
- Foreman::AppDoesNotExist
- Foreman::CLI
- Foreman::Distribution
- Foreman::Engine
- Foreman::Engine::CLI
- Foreman::Engine::CLI::Color
- Foreman::Export
- Foreman::Export::Base
- Foreman::Export::Bluepill
- Foreman::Export::Exception
- Foreman::Export::Inittab
- Foreman::Export::Launchd
- Foreman::Export::Runit
- Foreman::Export::Supervisord
- Foreman::Export::Upstart
- Foreman::Helpers
- Foreman::Process
- Foreman::Procfile

No matching classes.

# Foreman::Process

### Attributes

command[R]
env[R]

### Public Class Methods

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

Create a Process

@param [String] command The command to run @param [Hash] options

@option options [String] :cwd (./) Change to this working directory before executing the process @option options [Hash] :env ({}) Environment variables to set for this process

    # File lib/foreman/process.rb, line 16
    def initialize(command, options={})
      @command = command
      @options = options.dup

      @options[:env] ||= {}
    end

### Public Instance Methods

alive?() click to toggle source

Test whether or not this `Process` is still running

@returns [Boolean]

    # File lib/foreman/process.rb, line 105
    def alive?
      kill(0)
    end

cwd() click to toggle source

Returns the working directory for this `Process`

@returns [String]

    # File lib/foreman/process.rb, line 121
    def cwd
      File.expand_path(@options[:cwd] || ".")
    end

dead?() click to toggle source

Test whether or not this `Process` has terminated

@returns [Boolean]

    # File lib/foreman/process.rb, line 113
    def dead?
      !alive?
    end

exec(options={}) click to toggle source

Exec a `Process`

@param [Hash] options

@option options :env ({}) Environment variables to set for this execution

@return Does not return

    # File lib/foreman/process.rb, line 80
    def exec(options={})
      env = @options[:env].merge(options[:env] || {})
      env.each { |k, v| ENV[k] = v }
      Dir.chdir(cwd)
      Kernel.exec expanded_command(env)
    end

expanded_command(custom_env={}) click to toggle source

Get environment-expanded command for a `Process`

@param [Hash] custom_env ({}) Environment variables to merge with defaults

@return [String] The expanded command

    # File lib/foreman/process.rb, line 29
    def expanded_command(custom_env={})
      env = @options[:env].merge(custom_env)
      expanded_command = command.dup
      env.each do |key, val|
        expanded_command.gsub!("$#{key}", val)
      end
      expanded_command
    end

kill(signal) click to toggle source

Send a signal to this `Process`

@param [String] signal The signal to send

    # File lib/foreman/process.rb, line 91
    def kill(signal)
      if Foreman.windows?
        pid && Process.kill(signal, pid)
      else
        pid && Process.kill("-#{signal}", pid)
      end
    rescue Errno::ESRCH
      false
    end

run(options={}) click to toggle source

Run a `Process`

@param [Hash] options

@option options :env ({}) Environment variables to set for this execution @option options :output ($stdout) The output stream

@returns [Fixnum] pid The `pid` of the process

    # File lib/foreman/process.rb, line 47
    def run(options={})
      env    = @options[:env].merge(options[:env] || {})
      output = options[:output] || $stdout

      if Foreman.windows?
        Dir.chdir(cwd) do
          Process.spawn env, expanded_command(env), :out => output, :err => output
        end
      elsif Foreman.jruby_18?
        require "posix/spawn"
        wrapped_command = "#{Foreman.runner} -d '#{cwd}' -p -- #{command}"
        POSIX::Spawn.spawn env, wrapped_command, :out => output, :err => output
      elsif Foreman.ruby_18?
        fork do
          $stdout.reopen output
          $stderr.reopen output
          env.each { |k,v| ENV[k] = v }
          wrapped_command = "#{Foreman.runner} -d '#{cwd}' -p -- #{command}"
          Kernel.exec wrapped_command
        end
      else
        wrapped_command = "#{Foreman.runner} -d '#{cwd}' -p -- #{command}"
        Process.spawn env, wrapped_command, :out => output, :err => output
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.