Class: RBatch::Cmd
- Inherits:
-
Object
- Object
- RBatch::Cmd
- Defined in:
- lib/rbatch/cmd.rb
Overview
External command wrapper
Class Method Summary (collapse)
Instance Method Summary (collapse)
-
- (Cmd) initialize(cmd_str, opt = nil)
constructor
A new instance of Cmd.
-
- (RBatch::CmdResult) run
Run command.
Constructor Details
- (Cmd) initialize(cmd_str, opt = nil)
Returns a new instance of Cmd
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# 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 |
Class Method Details
+ (Object) def_vars=(vars)
14 |
# File 'lib/rbatch/cmd.rb', line 14 def Cmd.def_vars=(vars) ; @@def_vars=vars ; end |
Instance Method Details
- (RBatch::CmdResult) run
Run command
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# 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 |