Module: Angael::ProcessHelper
Instance Method Summary collapse
-
#exit_status(pid) ⇒ Object
Returns immediately.
-
#send_signal(signal, pid) ⇒ Object
Will return nil instead of raising Errno::ESRCH when the process does not exists.
Instance Method Details
#exit_status(pid) ⇒ Object
Returns immediately. If the process is still running, it returns nil. If the process is a zombie, it returns an array with the pid as the first element and a Process::Status object as the 2nd element, i.e. it returns the same thing as Process.wait2. If the process does not exist (i.e. it is completely gone) or it is not accessible to the current process (i.e. an Errno::EPERM error is raised), then this method returns an array with the pid as the first element and nil as the 2nd element (because there is no Process::Status object to return). If the argument is nil, then it returns [nil, nil] because there can’t be a process running with the pid nil.
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/angael/process_helper.rb', line 13 def exit_status(pid) return [nil, nil] if pid.nil? # Sometimes wait2 returns nil even when the process has exited. This # raises an Errno::ESRCH error in that case. raise "Unexpected return value from Process.kill(0, pid)" unless Process.kill(0, pid) == 1 Process.wait2(pid, Process::WNOHANG) rescue Errno::ECHILD, Errno::ESRCH, Errno::EPERM # There is no longer any record of this PID. # It did exit, but we don't know the exit status. [pid, nil] end |
#send_signal(signal, pid) ⇒ Object
Will return nil instead of raising Errno::ESRCH when the process does not exists. TODO: Add explicit tests for this.
31 32 33 34 35 |
# File 'lib/angael/process_helper.rb', line 31 def send_signal(signal, pid) Process.kill(signal, pid) rescue Errno::ESRCH nil end |