Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
Process.kill
static VALUE
proc_rb_f_kill(int c, const VALUE *v, VALUE _)
{
return rb_f_kill(c, v);
}
Sends a signal to each process specified by ids (which must specify at least one ID); returns the count of signals sent.
For each given id, if id is:
-
Positive, sends the signal to the process whose process ID is
id. -
Zero, send the signal to all processes in the current process group.
-
Negative, sends the signal to a system-dependent collection of processes.
Argument signal specifies the signal to be sent; the argument may be:
-
An integer signal number: e.g.,
-29,0,29. -
A signal name (string), with or without leading
'SIG', and with or without a further prefixed minus sign ('-'): e.g.:-
'SIGPOLL'. -
'POLL', -
'-SIGPOLL'. -
'-POLL'.
-
-
A signal symbol, with or without leading
'SIG', and with or without a further prefixed minus sign ('-'): e.g.:-
:SIGPOLL. -
:POLL. -
:'-SIGPOLL'. -
:'-POLL'.
-
If signal is:
-
A non-negative integer, or a signal name or symbol without prefixed
'-', each process with process IDidis signalled. -
A negative integer, or a signal name or symbol with prefixed
'-', each process group with group IDidis signalled.
Use method Signal.list to see which signals are supported by Ruby on the underlying platform; the method returns a hash of the string names and non-negative integer values of the supported signals. The size and content of the returned hash varies widely among platforms.
Additionally, signal 0 is useful to determine if the process exists.
Example:
pid = fork do Signal.trap('HUP') { puts 'Ouch!'; exit } # ... do some work ... end # ... Process.kill('HUP', pid) Process.wait
Output:
Ouch!
Exceptions:
-
Raises Errno::EINVAL or
RangeErrorifsignalis an integer but invalid. -
Raises
ArgumentErrorifsignalis a string or symbol but invalid. -
Raises Errno::ESRCH or
RangeErrorif one ofidsis invalid. -
Raises Errno::EPERM if needed permissions are not in force.
In the last two cases, signals may have been sent to some processes.