Method: MJ::Tools::SubProcess::ClassMethods#execute

Defined in:
lib/mj/tools/subprocess.rb

#execute(command, wd = ENV["HOME"], env = nil) ⇒ Object

Executes command

Executes the given command and yields each line of output. Returns $? .



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mj/tools/subprocess.rb', line 15

def execute( command, wd = ENV["HOME"], env = nil )
    logger.trace "(#{wd}) > #{command} 2>&1"
    if !$noop
        adjust_environment( wd, env ) {
            IO.popen( "#{command} 2>&1" ) {
                |f|
                begin
                    while
                        line = f.readline
                        if block_given?
                            yield line.chomp
                        else
                            logger.verbose line.chomp
                        end
                    end
                rescue EOFError
                    # Expected. Do nothing
                end
            }
            if $?.coredump? or $?.signaled?
                raise CoreDumpError, "Command '#{command}' core dumped because of signal #{$?.termsig}!"
            end
            logger.trace "= #{$?.exitstatus}"
            return $?.exitstatus
        }
    else
        adjust_environment( wd, env ) {
            logger.trace "= 0 # noop" 
        }
        return 0
    end

end