Skip to content

Latest commit

 

History

History
532 lines (337 loc) · 11.7 KB

kill_commands.md

File metadata and controls

532 lines (337 loc) · 11.7 KB

The kill command in Linux is used to terminate or send signals to processes. While it's commonly associated with killing processes, it can also be used for other purposes such as pausing or resuming processes by sending specific signals.


Basic Syntax

kill [options] <pid>...

Where <pid> is the Process ID of the process you want to interact with.


Basic Usage

The simplest usage of kill sends the SIGTERM (15) signal by default, which requests the process to terminate gracefully.

kill <pid>

Example:

kill 1234

This sends a SIGTERM to the process with PID 1234, asking it to terminate.


Common Signals with kill

The kill command allows sending different signals to processes. Here are some commonly used signals:

1. SIGTERM (15): Terminate Process (Gracefully)

This is the default signal sent by kill if no signal is specified. It requests the process to terminate gracefully.

kill 1234

2. SIGKILL (9): Force Terminate Process

This signal forcefully kills a process, immediately terminating it without allowing it to clean up or handle the signal.

kill -9 1234
  • Use: When a process doesn't terminate after receiving SIGTERM, you can forcefully kill it using SIGKILL.
  • Warning: This signal should be used with caution, as it can lead to data loss or corruption, since the process cannot handle cleanup.

3. SIGSTOP (19): Pause/Stop Process

This signal pauses a running process without terminating it. You can later resume it with the SIGCONT signal.

kill -19 1234
  • Use: Pausing a process temporarily.

4. SIGCONT (18): Resume Process

Resumes a paused process (one that received SIGSTOP).

kill -18 1234

5. SIGINT (2): Interrupt Process (like pressing Ctrl+C)

This signal interrupts a process, similar to pressing Ctrl+C in the terminal. It asks the process to terminate.

kill -2 1234

6. SIGQUIT (3): Quit Process (with core dump)

Similar to SIGINT, but also causes the process to dump core, which can be useful for debugging.

kill -3 1234

7. SIGHUP (1): Hang Up (typically for Daemons)

Used to tell a daemon process to reload its configuration without terminating it. Common for services like Apache or Nginx.

kill -1 1234
  • Use: When you want a process to re-read its configuration files.

8. SIGUSR1 and SIGUSR2 (10, 12): User-Defined Signals

These are custom signals that can be sent to processes to trigger specific actions defined by the process itself.

kill -10 1234
kill -12 1234

Options with kill

1. Send a Specific Signal Using -s

You can specify the signal to send using the -s option followed by the signal name or number:

kill -s SIGKILL 1234
kill -s 9 1234

Both of the above commands are equivalent to kill -9 1234, sending the SIGKILL signal.

2. Send Signal to Multiple Processes

You can send a signal to multiple processes by specifying multiple PIDs:

kill 1234 5678 91011

3. Kill All Processes with a Specific Name

Use the pkill command (a variant of kill) to send a signal to processes based on name. For example, to terminate all firefox processes:

pkill firefox
  • You can also use pkill to send different signals, such as pkill -9 firefox.

4. Send Signal to Processes by Group ID

You can send a signal to all processes in a process group using kill with the -g option:

kill -g <pgid> <signal>

Example:

kill -9 -g 1234

This sends a SIGKILL signal to all processes in the group with PGID 1234.


Viewing Processes and Their PIDs

Before using kill, you often need to know the PID of the process you want to terminate.

  • List all processes: Use the ps command to view all running processes and their PIDs.

    ps -e
  • Find a process by name: Use pgrep to search for a process by name and retrieve its PID(s).

    pgrep firefox
  • Show all processes in a tree format:

    ps aux --forest
  • List processes with detailed information:

    ps aux

Example Use Cases

1. Gracefully Terminate a Process

kill 1234
  • Terminates process 1234 by sending SIGTERM.

2. Forcefully Kill a Process

kill -9 1234
  • Forcefully kills process 1234 with SIGKILL.

3. Pause a Process

kill -19 1234
  • Pauses process 1234 using SIGSTOP.

4. Resume a Paused Process

kill -18 1234
  • Resumes process 1234 using SIGCONT.

5. Send a Custom Signal to a Process

kill -10 1234
  • Sends SIGUSR1 (signal 10) to process 1234, which can trigger a user-defined action.

Alternatives to kill

1. pkill

pkill is a more flexible command than kill because it allows you to send signals to processes based on their name, group, user, or other attributes.

pkill -9 firefox

This sends the SIGKILL signal to all processes named firefox.

2. killall

Similar to pkill, killall allows you to terminate processes by name. It is commonly used to kill all processes with a specific name.

killall -9 firefox

Important Notes

  • SIGTERM (15) is the default signal and is preferred for graceful termination, allowing processes to clean up resources.
  • SIGKILL (9) should be used as a last resort when a process does not respond to SIGTERM.
  • Always verify that you’re terminating the correct process using ps or pgrep before sending SIGKILL, as it may cause unintended consequences (e.g., data loss, system instability).

Let me know if you'd like additional examples or further details!

The kill command in Linux is used to terminate processes by sending specific signals to them. It is a vital tool for process management, allowing you to end misbehaving or unneeded processes.


What is kill?

The kill command sends a signal to a process. By default, it sends the SIGTERM signal, which politely requests the process to terminate. If a process doesn’t respond, you can use stronger signals like SIGKILL.


Basic Syntax

kill [options] <pid>
  • <pid>: Process ID of the target process.
  • Signals: Used to specify how the process should be handled (e.g., terminate, stop, restart).

Commonly Used Signals

Signal Number Description
SIGTERM 15 Politely asks a process to terminate. Default signal.
SIGKILL 9 Forcefully terminates a process. Cannot be ignored.
SIGHUP 1 Restarts the process (common for daemons).
SIGSTOP 19 Pauses (stops) a process.
SIGCONT 18 Resumes a paused process.
SIGUSR1 10 User-defined signal 1.
SIGUSR2 12 User-defined signal 2.

How to Use the kill Command

1. Find the Process ID (PID)

You need the PID of the process to use kill. Use ps, top, or pgrep to find it:

ps -ef | grep process_name

2. Send the Default Signal (SIGTERM)

To terminate a process gracefully:

kill <pid>

Example:

kill 1234

3. Forcefully Kill a Process (SIGKILL)

If the process doesn’t respond to SIGTERM:

kill -9 <pid>

Example:

kill -9 1234

4. Restart a Process (SIGHUP)

Some daemons and services restart when sent SIGHUP:

kill -1 <pid>

5. Pause and Resume a Process

  • Pause a process with SIGSTOP:
    kill -19 <pid>
  • Resume the process with SIGCONT:
    kill -18 <pid>

kill Command Variants

1. pkill: Kill Processes by Name

The pkill command allows you to kill processes by name instead of PID:

pkill process_name

Example:

pkill firefox

2. killall: Kill All Instances of a Process

To terminate all processes with a specific name:

killall process_name

Example:

killall apache2

Advanced Usage

1. Send Custom Signals

You can send specific signals by name or number:

kill -<signal_number> <pid>

or

kill -<signal_name> <pid>

Example:

kill -SIGUSR1 1234

or

kill -10 1234

2. Kill Multiple Processes

You can specify multiple PIDs to terminate:

kill 1234 5678 91011

3. Combine ps and kill

Find and kill a process in one command:

ps -ef | grep process_name | awk '{print $2}' | xargs kill

Signals in Depth

Signal Description
SIGTERM Allows the process to clean up and exit gracefully.
SIGKILL Immediately kills the process. Cannot be intercepted.
SIGHUP Often used to reload configuration files or restart daemons.
SIGINT Sent by pressing Ctrl+C in the terminal.
SIGQUIT Sent by pressing Ctrl+\. Generates a core dump.
SIGSTOP Stops (pauses) the process.
SIGCONT Continues a stopped process.
SIGUSR1/2 User-defined signals, typically used for custom actions.

Practical Examples

1. Kill All Processes Owned by a User

pkill -u username

2. Kill All Zombie Processes

Zombie processes have a status of Z. Find and kill them:

ps -eo pid,stat,cmd | grep Z | awk '{print $1}' | xargs kill -9

3. Kill a Background Job

If you’ve started a process in the background (&), use jobs to find it:

jobs
kill %<job_number>

Example:

kill %1

4. Gracefully Restart a Service

For processes that support SIGHUP:

kill -1 <pid>

Common Troubleshooting

Q1. What if kill doesn’t work?

  1. Process is stuck in an uninterruptible state (e.g., waiting for I/O). You must resolve the underlying issue (e.g., file system issue).
  2. Permission Denied: Ensure you have sufficient privileges (use sudo if necessary):
    sudo kill <pid>

Q2. How to Kill a Process on a Remote Server?

Use ssh to connect and then use kill:

ssh user@remote-server
kill <pid>

Key Differences Between kill, pkill, and killall

Command Description
kill Requires a specific PID to terminate a process.
pkill Terminates processes by name.
killall Terminates all processes matching a name.

Let me know if you want more examples or need help applying the kill command in specific scenarios!