Bash Terminate (kill)

Stop and manage running processes

⚡ What is kill Command?

The kill command sends signals to running processes, typically to stop them. It's used to terminate unresponsive programs, manage system resources, and control process behavior using various signal types.


# Terminate a process by PID
kill 1234

# Force kill a process
kill -9 1234
                                    

Output:

Process 1234 terminated successfully

Common kill Signals

🛑

SIGTERM (15)

Graceful termination (default)

kill 1234
💥

SIGKILL (9)

Force immediate termination

kill -9 1234
⏸️

SIGSTOP (19)

Pause process execution

kill -19 1234
▶️

SIGCONT (18)

Resume paused process

kill -18 1234

🔹 Basic Process Termination

The fundamental method to stop a process is using the kill command followed by the target's Process ID (PID). By default, kill sends the SIGTERM (signal 15) signal, which requests the process to terminate gracefully. This allows the program to perform cleanup operations—like closing files, saving state, and releasing resources—before shutting down. For example, kill 1234 asks process 1234 to exit. SIGTERM should always be the first attempt, as it respects the application's ability to shut down properly and avoids data corruption or incomplete operations.

# Find process ID first
ps aux | grep firefox

# Kill process by PID
kill 1234

# Kill multiple processes
kill 1234 5678 9012

Output:

user     1234  2.5  3.2  firefox
[1]+ Terminated    firefox

🔹 Force Kill Unresponsive Processes

When a process ignores SIGTERM or becomes completely frozen, the SIGKILL signal (sent with kill -9 or kill -KILL) forces immediate and unconditional termination. SIGKILL cannot be caught, blocked, or ignored by the process; the kernel stops it instantly without allowing any cleanup. Use this as a last resort for processes that are unresponsive, causing system instability, or consuming excessive resources. For example, kill -9 1234 will forcibly remove process 1234. Be aware that this can lead to open files not being closed cleanly or data loss within the application.

# Force kill a frozen process
kill -9 1234

# Alternative syntax
kill -SIGKILL 1234

# Force kill multiple processes
kill -9 1234 5678

Output:

[1]+ Killed    unresponsive_app

🔹 Kill by Process Name

Instead of looking up PIDs, you can terminate processes by name using killall or pkill, which match against the command name. killall firefox sends the default SIGTERM to all processes named "firefox". pkill offers more pattern-matching flexibility. This is convenient for stopping all instances of an application but requires caution: it can affect multiple processes simultaneously, potentially including ones you didn't intend to stop. Always verify which processes will be targeted, perhaps with pgrep -l firefox, before using these commands to avoid unintended service interruptions.

# Kill all processes with name
killall firefox

# Kill by partial name match
pkill fire

# Kill all user's processes of a program
killall -u username firefox

Output:

Terminated 3 firefox processes

🔹 List Available Signals

To see the complete list of signals that can be sent to processes, use the command kill -l (list). This displays all signal names and their corresponding numbers, such as SIGHUP (1), SIGINT (2), SIGQUIT (3), SIGKILL (9), SIGTERM (15), SIGSTOP (19), and SIGCONT (18). Each signal has a specific purpose, from termination (SIGTERM) to stopping execution (SIGSTOP) or reloading configuration (SIGHUP). Understanding this list allows you to control processes with precision, choosing the right signal for tasks like graceful shutdown, pausing, debugging, or configuration updates without restarting.

# List all signals
kill -l

# Show signal number
kill -l SIGTERM

# Show signal name
kill -l 15

Output:

 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL
 5) SIGTRAP      6) SIGABRT      7) SIGBUS       8) SIGFPE
 9) SIGKILL     10) SIGUSR1     11) SIGSEGV     12) SIGUSR2
13) SIGPIPE     14) SIGALRM     15) SIGTERM     16) SIGSTKFLT

🔹 Pause and Resume Processes

You can suspend a running process without terminating it using the SIGSTOP signal (e.g., kill -STOP 1234) and later resume it with SIGCONT (kill -CONT 1234). This pauses the process's execution, freeing its CPU cycles while keeping it loaded in memory. Pausing is useful for temporarily reducing system load, debugging a specific process state, or preventing a job from running during maintenance. When resumed, the process continues exactly from where it stopped. This is a more controlled alternative to killing, preserving the entire state of the application for later continuation.

# Pause a process
kill -SIGSTOP 1234
# or
kill -19 1234

# Resume the process
kill -SIGCONT 1234
# or
kill -18 1234

Output:

[1]+ Stopped    long_running_task
[1]+ Continued  long_running_task

🔹 Reload Configuration

Sending the SIGHUP signal (often with kill -HUP or kill -1) instructs many daemons and server processes to reload their configuration files without a full restart. This is a standard practice for applying new settings to services like web servers (nginx, Apache) or system daemons. The advantage over a restart is that it maintains active connections and current runtime state, minimizing service disruption. For example, kill -HUP $(cat /var/run/nginx.pid) makes Nginx re-read its config. Always check the process's documentation to confirm it supports graceful reload via SIGHUP.

# Reload configuration
kill -SIGHUP 1234
# or
kill -1 1234

# Reload web server
sudo kill -HUP $(cat /var/run/nginx.pid)

Output:

Configuration reloaded successfully

🔹 Practical Kill Examples

In daily system administration, the kill command family is used to manage processes in various real-world scenarios. Examples include: using pkill -f "python script.py" to stop a specific Python script; sending SIGTERM to a parent process and observing if child processes also exit cleanly; creating a script that uses kill -STOP to pause non-critical batch jobs during peak hours; and employing killall -u username to log out all processes for a specific user. These examples demonstrate moving beyond basic termination to orchestrated process control for stability and resource management.

# Find and kill a frozen browser
ps aux | grep chrome
kill -9 $(pgrep chrome)

# Kill all processes by user
pkill -u username

# Kill process tree (parent and children)
kill -TERM -1234

# Graceful shutdown with timeout
kill 1234 && sleep 5 && kill -9 1234

Output:

Terminated 5 chrome processes
Killed all processes for user: username
Process tree 1234 terminated

🔹 Safety Tips

⚠️ Important Warnings:

  • Check PID carefully: Killing wrong process can crash system
  • Try SIGTERM first: Use -9 only when necessary
  • Avoid killing system processes: PIDs below 100 are usually critical
  • Use killall carefully: It affects all matching processes
  • Backup data: Force kill may cause data loss

🧠 Test Your Knowledge

Which signal forcefully terminates a process?