Method: Process.kill
- Defined in:
- process.c
.kill(signal, pid, ...) ⇒ Fixnum
Sends the given signal to the specified process id(s), or to the current process if pid is zero. signal may be an integer signal number or a POSIX signal name (either with or without a SIG
prefix). If signal is negative (or starts with a minus sign), kills process groups instead of processes. Not all signals are available on all platforms.
pid = fork do
Signal.trap("HUP") { puts "Ouch!"; exit }
# ... do some work ...
end
# ...
Process.kill("HUP", pid)
Process.wait
produces:
Ouch!
If signal is an integer but wrong for signal, Errno::EINVAL
or RangeError
will be raised. Otherwise unless signal is a String
or a Symbol
, and a known signal name, ArgumentError
will be raised.
Also, Errno::ESRCH
or RangeError
for invalid pid, Errno::EPERM
when failed because of no privilege, will be raised. In these cases, signals may have been sent to preceding processes.
|
# File 'process.c'
VALUE
rb_f_kill(int argc, VALUE *argv)
{
#ifndef HAS_KILLPG
#define killpg(pg, sig) kill(-(pg), (sig))
#endif
int negative = 0;
int sig;
int i;
volatile VALUE str;
const char *s;
rb_secure(2);
if (argc < 2)
rb_raise(rb_eArgError, "wrong number of arguments (%d for at least 2)", argc);
switch (TYPE(argv[0])) {
case T_FIXNUM:
sig = FIX2INT(argv[0]);
break;
case T_SYMBOL:
s = rb_id2name(SYM2ID(argv[0]));
if (!s) rb_raise(rb_eArgError, "bad signal");
goto str_signal;
case T_STRING:
s = RSTRING_PTR(argv[0]);
str_signal:
if (s[0] == '-') {
negative++;
s++;
}
|