Public: Execute shell command.
iv - A String value for the IV file. cmd - A String value of the command to run. pwd - A String value of target working directory. ignore_err - A Boolean value to determine if errors should be ignored. expected_rc - A Integer value for the expected return code of cmd.
Examples
StickShift::Utils::ShellExec.shellCmd('ls /etc/passwd') # => ["/etc/passwd\n","", 0]
Returns An Array with [stdout, stderr, return_code]
# File lib/stickshift-node/utils/shell_exec.rb, line 49 def self.shellCmd(cmd, pwd = ".", ignore_err = true, expected_rc = 0, timeout = 3600) out = err = rc = nil begin # Using Open4 spawn with cwd isn't thread safe m_cmd = "cd #{pwd} && ( #{cmd} )" pid, stdin, stdout, stderr = Open4.popen4ext(true, m_cmd) begin stdin.close Timeout::timeout(timeout) do out = stdout.read err = stderr.read end rescue Timeout::Error pstree = Hash.new{|a,b| a[b]=[b]} pppids = Hash[*%xps -e -opid,ppid --no-headers`.map{|p| p.to_i}] pppids.each do |l_pid, l_ppid| pstree[l_ppid] << pstree[l_pid] end Process.kill("KILL", *(pstree[pid].flatten)) raise StickShift::Utils::ShellExecutionException.new("command timed out") ensure stdout.close stderr.close rc = Process::waitpid2(pid)[1].exitstatus end rescue Exception => e raise StickShift::Utils::ShellExecutionException.new(e.message ) unless ignore_err end if !ignore_err and rc != expected_rc raise StickShift::Utils::ShellExecutionException.new( "Shell command '#{cmd}' returned an error. rc=#{rc}", rc) end return [out, err, rc] end
# File lib/stickshift-node/utils/shell_exec.rb, line 32 def shellCmd(cmd, pwd = ".", ignore_err = true, expected_rc = 0, timeout = 3600) StickShift::Utils::ShellExec.shellCmd(cmd, pwd, ignore_err, expected_rc, timeout) end