Class: ShellTastic::IO
- Inherits:
-
Object
- Object
- ShellTastic::IO
- Extended by:
- Utils
- Defined in:
- lib/shelltastic/command_io.rb
Class Method Summary collapse
-
.fire_and_forget(command, timer, formatter) ⇒ Hash
run command in background dont wait for it to exit.
-
.popen(command, timer, formatter) ⇒ Hash
A wrapper around popen4.
Methods included from Utils
empty_nil_blank?, string_nil_or_blank!, string_nil_or_blank?
Class Method Details
.fire_and_forget(command, timer, formatter) ⇒ Hash
Note:
output, error , exitstatus will be nil
run command in background dont wait for it to exit
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/shelltastic/command_io.rb', line 51 def fire_and_forget(command, timer, formatter) string_nil_or_blank! command begin formatter.start = timer.start pid = Process.spawn(command, :pgroup=>true, [:out, :err, :in] => "/dev/null") formatter.build(command: command, output: nil, pid: pid, error: false, stop: timer.stop, exitstatus: $?, total_time: timer.total_time) Process.detach(pid) formatter.inspect rescue Errno::ENOENT => e Process.kill(9, pid) if pid raise ShellTastic::CommandException.new("Shell command #{command} failed with status #{$?} and ERROR: #{e.message}") ensure Process.detach(pid) if pid end end |
.popen(command, timer, formatter) ⇒ Hash
A wrapper around popen4
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/shelltastic/command_io.rb', line 19 def popen(command, timer, formatter) string_nil_or_blank! command begin formatter.start = timer.start pid, stdin, stdout, stderr = Open4::popen4(command) stdin.close _, status = Process::waitpid2 pid error = stderr.read.strip formatter.build(command: command, output: stdout.read.strip, pid: pid, error: string_nil_or_blank?(error) ? false : error, stop: timer.stop, exitstatus: status.exitstatus, total_time: timer.total_time) rescue Errno::ENOENT => e raise ShellTastic::CommandException.new("Shell command #{command} failed with status #{$?} and ERROR: #{e.message}") end formatter.inspect end |