Overview of kill
`kill` command sends a signal to a process with a specific Process ID (PID). The most common signal is to terminate the process, but depending on the type of signal, the process may react differently. For example, it can pause a process or request it to reload its configuration file.
Importance of Process ID (PID)
`kill` command primarily takes the PID as an argument. Therefore, it is important to know the exact PID of the process you want to control. You can check the PID of a process using the `ps` command or the `top` command.
Key Roles of kill
- Terminate Processes: Forcefully or normally terminates processes that are no longer needed or malfunctioning.
- Restart/Reload Processes: Sends a specific signal to prompt the process to read its configuration file again or to restart.
- Manage System Resources: Terminates processes that consume unnecessarily high resources to ensure system stability.
Most Commonly Used Signals
There are various signals, but the following two are the most commonly used:
Key Signals
- **SIGTERM (15, default)**: Sends a gentle request to the process to 'terminate'. When the process receives this signal, it can perform cleanup tasks and terminate. In most cases, this signal is tried first.
- **SIGKILL (9)**: Immediately forces the process to terminate. The process cannot ignore or catch this signal and is terminated without the opportunity to perform cleanup tasks. It should be used as a last resort.
Key Options for kill Command
`kill` command controls processes using signal numbers or signal names. When sending signals to a specific process, the PID of that process must be specified accurately.
1. Basic Usage
2. Useful Signals
3. View All Signal List
Generated command:
Try combining the commands.
Description:
`kill` Executes the command.
Combine the above options to virtually execute commands with AI.
killall Command (Terminate by Name)
`killall` command sends signals to all processes matching a name rather than a PID, unlike `kill`. This is very convenient for terminating all instances of a specific application.
Normal Termination by Name
killall nginx
Sends a SIGTERM signal to all processes named `nginx` requesting normal termination.
Force Termination by Name
killall -9 firefox
Sends a SIGKILL signal to all processes named `firefox` for immediate termination.
Terminate Only Specific User's Processes
killall -u $(whoami) chrome
Terminates only the `chrome` processes of the current user. Other users' `chrome` processes are unaffected.
pkill Command (Terminate by Pattern)
`pkill` command is similar to `killall`, but it uses regular expression patterns to find processes, making it more flexible. This is useful for finding processes by partial names or terminating multiple related processes at once.
Normal Termination by Pattern
pkill http
Sends a SIGTERM signal to all processes containing 'http' in their names to terminate. (e.g., `httpd`, `apache2`, etc.)
Force Termination by Pattern
pkill -9 mysql
Sends a SIGKILL signal to all processes containing 'mysql' in their names for forceful termination.
Terminate Processes Belonging to Specific User
pkill -u www-data php-fpm
Terminates all 'php-fpm' processes belonging to the `www-data` user.
Usage Examples
Learn how to effectively manage system processes through various usage examples of the `kill`, `killall`, and `pkill` commands.
Terminate Specific Process ID (PID)
kill 12345
Sends a SIGTERM signal to the process with PID `12345` to attempt a normal termination.
Force Termination of Process (Using PID)
kill -9 54321
If the process with PID `54321` is unresponsive, force termination using the `SIGKILL` signal.
Reload Nginx Process (Using SIGHUP)
sudo kill -1 $(cat /run/nginx.pid)
Finds the master process (PID) of Nginx and sends a `SIGHUP` signal to read the configuration file again. (Nginx process PID can be checked with `ps aux | grep nginx` etc.)
Terminate All Chrome Browsers by Name
killall chrome
Normally terminates all running Chrome browser processes. (Note: unsaved content may be lost when windows close.)
Terminate All Processes of User 'johndoe'
sudo killall -9 -u johndoe
Forcefully terminates all processes run by the user `johndoe`. (Admin privileges required)
Terminate CPU-Intensive Process (Example)
# 1. Check PID using top or htop (e.g., 12345)
# 2. Terminate the corresponding PID
kill 12345
A common scenario to check the PID of a CPU-intensive process using `top` or `htop`, and then terminate it.