Class: CMD
- Inherits:
-
Hash
- Object
- Hash
- CMD
- Defined in:
- lib/cmd.rb,
lib/cmd_windows.rb
Class Method Summary collapse
Instance Method Summary collapse
- #call_capture ⇒ Object
- #call_popen ⇒ Object
- #command_pid ⇒ Object
- #execute ⇒ Object
- #execute_as(username) ⇒ Object
- #get_child_processes(pid) ⇒ Object
-
#initialize(cmd, options = nil) ⇒ CMD
constructor
A new instance of CMD.
- #interrupt ⇒ Object
Constructor Details
#initialize(cmd, options = nil) ⇒ CMD
Returns a new instance of CMD.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/cmd.rb', line 10 def initialize(cmd, =nil) initialize_defaults if(.nil?) self[:output] = '' self[:error] = '' #1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL #5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE #9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 #13) SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD #18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN #22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ #26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO #30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 #36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 #40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 #44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 #48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 #52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 #56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 #60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 #64) SIGRTMAX self[:timeout_signal] = 2 self[:timeout_raise_error] = true .each { |key, value| self[key] = value} .each { |key, value| self[key] = value} unless(.nil?) self[:command]=cmd end |
Class Method Details
.default_options(hash) ⇒ Object
40 41 42 |
# File 'lib/cmd.rb', line 40 def self.(hash) hash.each { |key, value| [key] = value} end |
Instance Method Details
#call_capture ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/cmd.rb', line 124 def call_capture elapsed_time = nil begin if(key?(:timeout)) start_time = Time.now Thread.new do while !key?(:exit_code) do sleep(0.1) if((Time.now - start_time).to_f > self[:timeout]) self[:timed_out] = true interrupt sleep(0.1) break end end end end self[:output] = self[:error] = '' elapsed_time = Benchmark.realtime do self[:output], self[:error], status = Open3.capture3(self[:command]) self[:exit_code] = status.to_i end raise TimeoutError.new("Command '#{self[:command]}' timed out after #{self[:timeout]} seconds") if(key?(:timed_out) && self[:timeout_raise_error]) rescue Exception => e self[:error] = "#{self[:error]}\nException: #{e.to_s}" self[:exit_code]=1 unless(!self[:exit_code].nil? || (self[:exit_code] == 0)) ensure self[:elapsed_time] = elapsed_time unless(elapsed_time.nil?) end end |
#call_popen ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/cmd.rb', line 71 def call_popen begin output = '' error = '' Thread.abort_on_exception = true mutex = Mutex.new Open3.popen3(self[:command]) do |stdin, stdout, stderr, wait_thr| self[:pid] = wait_thr.pid if(key?(:timeout)) start_time = Time.now Thread.new do while wait_thr.alive? do sleep(0.1) if((Time.now - start_time).to_f > self[:timeout]) self[:timed_out] = true interrupt sleep(0.1) end end end end {:output => stdout,:error => stderr}.each do |key, stream| Thread.new do while wait_thr.alive? && !key?(:timed_out) do if(!stream.closed? && !(char = stream.getc).nil?) case key when :output output << char putc char if(self[:echo_output]) when :error error << char end else sleep(0.1) end end end end wait_thr.join self[:output] = output unless(output.empty?) self[:error] = error unless(error.empty?) self[:exit_code] = wait_thr.value.to_i end rescue Exception => e self[:error] = "#{self[:error]}\nException: #{e.to_s}" self[:exit_code]=1 unless(self[:exit_code].nil? || (self[:exit_code] == 0)) end end |
#command_pid ⇒ Object
156 157 158 159 160 161 162 163 164 |
# File 'lib/cmd.rb', line 156 def command_pid Sys::ProcTable.ps do |p| if(p.ppid == $$) return self[:pid] = p.pid end end raise "Failed to find child process for command: '#{self[:command]}'" end |
#execute ⇒ Object
44 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 |
# File 'lib/cmd.rb', line 44 def execute if(self[:quiet]) self[:echo_output] = false self[:echo_command] = false end puts self[:command] if(self[:echo_command] || self[:debug]) #call_popen call_capture if(self[:debug]) puts "command: #{self[:command]}" puts "output: #{self[:output]}" puts "error: #{self[:error]}" puts "exit_code: #{self[:exit_code]}" end raise TimeoutError.new("Command '#{self[:command]}' timed out after #{self[:timeout]} seconds") if(key?(:timed_out) && self[:timeout_raise_error]) if((self[:exit_code] != 0) && !self[:ignore_exit_code]) exception_text = "Exit code: #{self[:exit_code]}" exception_text = "#{exception_text}\nError: '#{self[:error]}'" exception_text = "#{exception_text}\nOutput: '#{self[:output]}'" raise StandardError.new(exception_text) end end |
#execute_as(username) ⇒ Object
6 7 8 9 |
# File 'lib/cmd_windows.rb', line 6 def execute_as(username) self[:command] = "runas /noprofile /savecred /user:#{username} \"#{self[:command]}\"" wait_on_spawned_process(self) { self.execute } end |
#get_child_processes(pid) ⇒ Object
176 177 178 179 180 181 182 183 184 185 |
# File 'lib/cmd.rb', line 176 def get_child_processes(pid) processes = [] Sys::ProcTable.ps do |p| if(p.ppid == pid) get_child_processes(p.pid).each { |cp| processes << cp } processes << p end end return processes end |
#interrupt ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/cmd.rb', line 165 def interrupt self[:pid] = command_pid unless(key?(:pid)) raise "Do not have a process id for #{self[:pid]}" unless(key?(:pid)) processes = get_child_processes(self[:pid]) Process.kill(self[:timeout_signal],self[:pid]) sleep(0.1) processes.each { |p| Process.kill(self[:timeout_signal],p.pid) unless(Sys::ProcTable.ps(p.pid).nil?) } # #Process.waitpid(self[:pid]) unless(Sys::ProcTable.ps(self[:pid]).nil?) end |