kill - Linux


Overview

The kill command in Linux is used for terminating processes manually. This command sends signals to programs identified by their process identifiers (PIDs). While the most common use of kill is to stop a process, it can send any signal to any process, making it a versatile tool for process management.

Syntax

The basic syntax for the kill command is:

kill [options] <pid> [...]

Where <pid> is the process ID of the target process. Multiple PIDs can be specified to send signals to more than one process at a time.

Options/Flags

  • -l or --list=[signal]: List all signal names. If a signal is specified, it will show the signal number of the specific signal.
  • -s <signal> or --signal <signal>: Specify the signal to send. Signals can be specified by name (SIGINT) or by number (2). If omitted, SIGTERM (15) is the default signal.
  • -p or --pid: Do not send any signals, but list the processes that would be affected.
  • --verbose: Provide verbose output, showing what is being done.

Example Flags Usage:

  • To list all available signals, use: kill -l
  • To send a SIGKILL signal, you might use: kill -s SIGKILL <pid> or kill -9 <pid>

Examples

  1. Terminate a process with a default signal (SIGTERM):
    kill 1234
    
  2. Send a SIGKILL to terminate process immediately:
    kill -9 1234
    
  3. List all signals:
    kill -l
    
  4. Use verbose mode to explain what is being done:
    kill --verbose -s SIGINT 1234
    

Common Issues

  1. Permission Denied: Trying to kill a process owned by another user or system process without sufficient privileges.
    • Solution: Use sudo to run the kill command.
  2. Invalid PID: Specified PID does not exist.
    • Solution: Ensure the correct PID is used; use commands like ps to find correct PIDs.

Integration

kill can be combined with other commands for more powerful process management. For example:

ps aux | grep 'node' | awk '{print $2}' | xargs kill

This pipeline finds all ‘node’ processes, extracts their PIDs, and passes them to kill to terminate the processes.

  • killall: Kill processes by name.
  • pkill: Send a signal based on process attributes like the name.
  • top or htop: Interactive process viewers where you can send signals to processes.

For further reading and advanced topics related to process management, you can consult the official GNU documentation on core utilities.