class RBatch::Cmd
External command wrapper
Public Class Methods
def_vars=(vars)
click to toggle source
@private @param [RBatch::Variables] vars
# File lib/rbatch/cmd.rb, line 14 def Cmd.def_vars=(vars) ; @@def_vars=vars ; end
new(cmd_str,opt = nil)
click to toggle source
@param [String] cmd_str command string such as “ls -l” @option opt [Boolean] :raise @option opt [Integer] :timeout
# File lib/rbatch/cmd.rb, line 28 def initialize(cmd_str,opt = nil) raise(CmdException,"Command string is nil") if cmd_str.nil? @cmd_str = cmd_str @vars = @@def_vars.clone if ! opt.nil? # change opt key from "hoge" to "log_hoge" tmp = {} opt.each_key do |key| tmp[("cmd_" + key.to_s).to_sym] = opt[key] end @vars.merge!(tmp) end end
Public Instance Methods
run()
click to toggle source
Run command @raise [RBatch::CmdException] @return [RBatch::CmdResult]
# File lib/rbatch/cmd.rb, line 45 def run() stdout_file = Tempfile::new("rbatch_tmpout",Dir.tmpdir) stderr_file = Tempfile::new("rbatch_tmperr",Dir.tmpdir) pid = spawn(@cmd_str,:out => [stdout_file,"w"],:err => [stderr_file,"w"]) status = nil if @vars[:cmd_timeout] != 0 begin timeout(@vars[:cmd_timeout]) do status = Process.waitpid2(pid)[1] >> 8 end rescue Timeout::Error => e begin Process.kill('SIGINT', pid) raise(CmdException,"Run time of command \"#{@cmd_str}\" is over #{@vars[:cmd_timeout].to_s} sec. Success to kill process : PID=#{pid}" ) rescue raise(CmdException,"Run time of command \"#{@cmd_str}\" is over #{@vars[:cmd_timeout].to_s} sec. But Fail to kill process : PID=#{pid}" ) end end else status = Process.waitpid2(pid)[1] >> 8 end result = RBatch::CmdResult.new(stdout_file,stderr_file,status,@cmd_str) if @vars[:cmd_raise] && status != 0 raise(CmdException,"Command exit status is not 0. result: " + result.to_s) end return result end