kill - macOS


Overview

The kill command in macOS is used to send signals to running processes, typically for terminating processes. It can be utilized by system administrators and users to manage system or application processes that are not responding or behaving as expected. The kill command is most effective in managing system resources and can be crucial in scripts and batch jobs to control process execution.

Syntax

The basic syntax of the kill command is:

kill [options] <pid>
  • <pid> refers to the process identifier numbers.

For more advanced usage, the syntax can vary as:

kill -s <signal> <pid>
kill -l [signal]

Parameters:

  • -s <signal>: Specifies the signal to be sent.
  • -l: Lists all the signal names when used alone. When followed by signal, it will return the signal number.

Options/Flags

  • -s <signal>: Specify the signal to send to the process. The signal can be a name like HUP or a number like 1.
  • -l: When used alone, it lists all signal names, making it easier to identify signals by name or number.
  • -L: Synonym for -l.

Typical Signals

  • TERM (15): Gracefully terminate the process. This is the default signal if none is specified.
  • KILL (9): Forcibly shuts down the process. Cannot be intercepted or ignored by the process.
  • HUP (1): Hang up, primarily used to tell a process to reload its configuration.

Examples

Terminate a process using the default TERM signal:

kill 1234

Forcefully terminate a process using the KILL signal:

kill -s KILL 1234

or

kill -9 1234

List all available signals:

kill -l

Send a HUP signal to tell a process to refresh its configuration:

kill -HUP 2210

Common Issues

Invalid process ID:
Using an invalid or non-existent PID will return an error. Verifying the PID through commands like ps or top can prevent this.

Permission denied:
Trying to kill a process owned by another user or system processes without sufficient privileges will fail. Running kill with sudo might be required for such processes.

Integration

kill can be integrated with other commands, such as ps, grep, and loops. Here’s an example script to kill all processes with a specific name:

ps aux | grep 'process_name' | grep -v 'grep' | awk '{print $2}' | xargs kill
  • killall: Kills processes by name, rather than by PID.
  • pkill: Kills processes based on pattern matching.

For further reading, you can consult the official macOS documentation or man page (man kill) for detailed information on signals and process management.