Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

class method

Process.kill

(::Integer | ::interned signal, *::Integer pids) -> ::Integer
Process.kill(signal, *ids) → count

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 ID id is signalled.

  • A negative integer, or a signal name or symbol with prefixed '-', each process group with group ID id is 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:

Example
pid = fork do
  Signal.trap('HUP') { puts 'Ouch!'; exit }
  # ... do some work ...
end
# ...
Process.kill('HUP', pid)
Process.wait

Output:

Example
Ouch!

Exceptions:

In the last two cases, signals may have been sent to some processes.

See also .initgroups .last_status .groups= .maxgroups also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0