External command runcher.
Input cmd_params into Kernel#spawn.
Write command’s stdout and stderr to tmp file.
* If Platform is "mswin" or "mingw" , then temp directory is ENV["TEMP"] * If Platform is "linux" or "cygwin" , then temp directory is "/tmp/"
Return hash object including stdout, stderr, and exit status.
require 'rbatch' cmd = RBatch::Cmd("ls") r = cmd.run p r.stdout => "fileA\nfileB\n"
cmd = RBatch::Cmd("ls", {:verbose => true}) r = cmd.run
require 'rbatch' r = RBatch::cmd("ls") p r.stdout => "fileA\nfileB\n"
Cmd instance
cmd_str = Command string. Such ad “ls -l” opt = Option hash object. Hash keys is follows.
:raise (Boolean) = If command exit status is not 0, raise exception. Default is false.
:timeout (Integer) = If command timeout , raise exception. Default is 0 sec ( 0 means disable) .
# File lib/rbatch/cmd.rb, line 46 46: def initialize(cmd_str,opt = nil) 47: raise(CmdException,"Command string is nil") if cmd_str.nil? 48: @cmd_str = cmd_str 49: # parse option 50: @opt = @@def_opt.clone 51: @@def_opt.each_key do |key| 52: if opt != nil && opt[key] != nil 53: # use argument 54: @opt[key] = opt[key] 55: elsif RBatch.common_config != nil && RBatch.common_config["cmd_" + key.to_s] != nil 56: # use config 57: @opt[key] = RBatch.common_config["cmd_" + key.to_s] 58: else 59: # use default 60: end 61: end 62: end
Run command
instance of RBatch::CmdResult
# File lib/rbatch/cmd.rb, line 69 69: def run() 70: stdout_file = Tempfile::new("rbatch_tmpout",RBatch::tmp_dir) 71: stderr_file = Tempfile::new("rbatch_tmperr",RBatch::tmp_dir) 72: pid = spawn(@cmd_str,:out => [stdout_file,"w"],:err => [stderr_file,"w"]) 73: if @opt[:timeout] != 0 74: timeout(@opt[:timeout]) do 75: begin 76: status = Process.waitpid2(pid)[1] >> 8 77: rescue Timeout::Error => e 78: raise(CmdException,"Command timeout (over " + @opt[:timeout] + " sec)" ) 79: end 80: end 81: else 82: status = Process.waitpid2(pid)[1] >> 8 83: end 84: result = RBatch::CmdResult.new(stdout_file,stderr_file,status,@cmd_str) 85: if @opt[:raise] && status != 0 86: raise(CmdException,"Command exit status is not 0. result: " + result.to_s) 87: end 88: return result 89: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.