Module: Uc::ShellHelper
Instance Method Summary collapse
- #cmd(command, error_msg: nil, return_output: false) ⇒ Object
- #kill(pid, timeout) ⇒ Object
- #process_running?(pid) ⇒ Boolean
Instance Method Details
#cmd(command, error_msg: nil, return_output: false) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/uc/shell_helper.rb', line 4 def cmd(command, error_msg: nil, return_output: false) puts "#{"Running".bold.green} #{command}" if return_output output = `#{command}` else output = "" system "#{command} 2>&1" end return_value = $?.exitstatus error_msg ||= "Non zero exit for \"#{command}\"" raise ::Uc::Error, error_msg if return_value !=0 return output end |
#kill(pid, timeout) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/uc/shell_helper.rb', line 18 def kill(pid, timeout) Process.kill(:TERM, pid) logger.debug "TERM signal sent to #{pid}" (1..timeout).each do if not process_running? pid logger.info "Stopped #{pid}" return end sleep 1 end Process.kill(9, pid) sleep 1 logger.info "Killed #{pid}" end |
#process_running?(pid) ⇒ Boolean
33 34 35 36 37 38 39 |
# File 'lib/uc/shell_helper.rb', line 33 def process_running?(pid) return false if pid <= 0 Process.getpgid pid return true rescue Errno::ESRCH return false end |