Parent

Methods

RBatch::Cmd

 External command runcher.

This module is a wrapper of Kernel#spawn.

 * Arguments(cmd_params) are inputed to Kernel#spawn directly and run command.
 * Return an object of RBatch::CmdResult which includes stdout, stderr, and exit status.

 ==== Sample 1
  require 'rbatch'
  result = RBatch::cmd("ls")
  p result.stdout
  => "fileA\nfileB\n"

 ==== Sample 2 (use option)
  require 'rbatch'
  result = RBatch::cmd("ls",{:timeout => 1})
  p result.stdout
  => "fileA\nfileB\n"

 ==== Sample 3 (use instance)
  require 'rbatch'
  cmd = RBatch::Cmd.new("ls")
  result = cmd.run
  p result.stdout
  => "fileA\nfileB\n"

Public Class Methods

new(cmd_str,opt = nil) click to toggle source

Cmd instance

Params

cmd_str = Command string such as “ls -l” opt = Option hash object.

  • :raise (Boolean) = If command exit status is not 0, raise exception. Default is false.

  • :timeout (Integer) = If command timeout , raise exception and kill process. Default is 0 sec ( 0 means disable) .

    # File lib/rbatch/cmd.rb, line 44
44:     def initialize(cmd_str,opt = nil)
45:       raise(CmdException,"Command string is nil") if cmd_str.nil?
46:       @cmd_str = cmd_str
47:       tmp = {}
48:       if opt.nil?
49:         @opt=RBatch.run_conf.clone
50:       else
51:         opt.each_key do |key|
52:           tmp[("cmd_" + key.to_s).to_sym] = opt[key]
53:         end
54:         @opt=RBatch.run_conf.merge(tmp)
55:       end
56:     end

Public Instance Methods

run() click to toggle source

Run command

Return

instance of RBatch::CmdResult

    # File lib/rbatch/cmd.rb, line 62
62:     def run()
63:       stdout_file = Tempfile::new("rbatch_tmpout",Dir.tmpdir)
64:       stderr_file = Tempfile::new("rbatch_tmperr",Dir.tmpdir)
65:       pid = spawn(@cmd_str,:out => [stdout_file,"w"],:err => [stderr_file,"w"])
66:       status = nil
67:       if @opt[:cmd_timeout] != 0
68:         begin
69:           timeout(@opt[:cmd_timeout]) do
70:             status =  Process.waitpid2(pid)[1] >> 8
71:           end
72:         rescue Timeout::Error => e
73:           begin
74:             Process.kill('SIGINT', pid)
75:             raise(CmdException,"Run time of command \"#{@cmd_str}\" is over #{@opt[:cmd_timeout].to_s} sec. Success to kill process : PID=#{pid}" )
76:           rescue
77:             raise(CmdException,"Run time of command \"#{@cmd_str}\" is over #{@opt[:cmd_timeout].to_s} sec. Fail to kill process : PID=#{pid}" )
78:           end
79:         end
80:       else
81:         status =  Process.waitpid2(pid)[1] >> 8
82:       end
83:       result = RBatch::CmdResult.new(stdout_file,stderr_file,status,@cmd_str)
84:       if @opt[:cmd_raise] && status != 0
85:         raise(CmdException,"Command exit status is not 0. result: " + result.to_s)
86:       end
87:       return result
88:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.