Come chat with us and we will get back to you as soon as possible!
Contact SupportHolyHosting
Holy Team

When managing a Linux server, you may encounter unresponsive or malfunctioning processes that consume system resources, slow down your work and negatively impact other running processes. Knowing how to kill a process in Linux is essential for efficient system administration.
This article will guide you through various methods to terminate unresponsive processes in Linux. Whether you are a beginner or an experienced administrator, these methods will help you manage processes effectively in a Linux environment.
What is the kill Process in Linux?
The kill command in Linux terminates a running, unresponsive or malfunctioning process in a controlled and safe manner.
How to Locate a Process in Linux
Before terminating a process, it is crucial to identify it correctly. Fortunately, the Linux system provides various tools for this purpose. The ps and top commands are some of the most common.
The ps command displays a snapshot of all running processes, allowing you to find the ID (PID) of a specific process. Here is a basic format of the ps command:
```bash
ps [options]
```
The most common options to combine with the ps command are:
- `-a`: shows processes for all users.
- `-u`: shows the current user's processes.
- `-x`: includes processes without a terminal.
To make the list more specific, add the grep command. For example, the following command will show all running Java processes:
```bash
ps -aux | grep java
```
On the other hand, the top command offers a dynamic view of all running processes in Linux and their resource usage. It does not require additional arguments or options for its main use.
When ps and top are not specific enough, pidof and pgrep offer more focused search capabilities.
The pidof command finds the PID of a process by its name. Here is the basic format of the command:
```bash
pidof [opciones] [nombreproceso]
```
For example, `pidof nginx` finds the PID of NGINX processes. Some options can be included, such as:
- `-c`: ensures that only PIDs from the current root directory are returned.
- `-o`: omits the specified PIDs from the results.
- `-s`: returns only one PID, typically the oldest, among matching processes.
The pgrep command offers a more flexible search mechanism by name or other properties. Use the following format:
```bash
pgrep [opciones] [patrón]
```
For example, `pgrep -u username` locates processes run by a specific user. You can add these options to filter the output:
- `-n`: returns only the newest instance among matching processes.
- `-o`: returns only the oldest instance among matching processes.
- `-u`: matches processes owned by the specified user.
kill Command Signals
After identifying which process to terminate, the next step is to send an appropriate signal using the kill command. Each signal type serves a different purpose and affects how a process terminates.
To see all available signals, use the following command:
```bash
kill -l
```
This lists all Linux process signal types, including:
- `SIGTERM (15)`: this is the default and safest way to terminate a running process in Linux. It allows the process to terminate gracefully.
- `SIGKILL (9)`: this signal immediately stops any main or background process without allowing it to clean up.
- `SIGSTOP (19)`: pauses a current process without terminating it.
- `SIGHUP (1)`: this signal indicates that the user's terminal has disconnected, often leading to process termination.
- `SIGINT (2)`: this signal interrupts a process, typically sent from the Ctrl+C key combination.
When using kill signals, you can specify the signal name or its numeric value. It is crucial to choose the right kill signal based on the scenario and the impact you want on the process.
For example, the SIGTERM signal is preferred for safe process control in Linux, allowing processes to save their state and exit cleanly. SIGKILL, while practical, should be used sparingly as it can lead to data loss or corruption.
How to Kill a Process in Linux
After understanding the signals, let us explore the methods to kill a running process in Linux. If you use a virtual private server (VPS), access the server with an SSH client, such as PuTTY.
Holy VPS clients can use our built-in browser terminal for efficient process termination. Available for all VPS hosting plans, this tool provides a convenient way to manage your server directly from the browser.
How to Kill a Process Using Holy VPS
Here is how you can kill a process through hPanel:
1. Log in to hPanel and go to the VPS section.
2. Select the server you want to manage and access the SSH access tab. You will find the SSH username and password needed to enter the terminal.
3. Click the Browser Terminal button. This will open a new terminal window in a new tab.
4. Log in to the server by typing the SSH credentials you have obtained.
5. After logging in, you can use the kill command to terminate processes. For example:
```bash
kill 63772
```
Where 63772 is the PID of the process you want to close. You can also use other methods provided in the following sections in the Holy browser terminal.
Once you are done, access the floating Hide/Show control bar button and click Disconnect to stop the connection.
How to Kill a Process Using the kill Command with a PID
Killing a single process in Linux is simple when you know its PID. Run the command `kill [PID]`, for example:
```bash
kill 123
45
```
This will send a SIGTERM signal to the process with PID 12345, attempting to terminate it gracefully. It is beneficial for killing a background process in Linux.
If the process does not terminate with the default SIGTERM, you may need to force kill the process. This is done by adding the `-9` option, which sends a SIGKILL signal. For example:
```bash
kill -9 12345
```
How to Kill Multiple Processes
You can kill multiple processes at once in Linux using the same kill command. Pass multiple PIDs to the command, for example:
```bash
kill 12345 67890 13579
```
This simultaneously sends a termination signal to process PIDs 12345, 67890 and 13579.
In situations where you need to kill processes with similar characteristics, you can use wildcards with commands like pgrep. For example:
```bash
kill $(pgrep patrón)
```
This command will kill all processes matching the given pattern.
How to Kill a Process Using the pkill Command
The pkill command in Linux offers a more flexible approach to killing processes by name or other characteristics. It is advantageous when you do not know a specific PID.
To force kill a process by name in Linux, run `pkill [processname]`. For example:
```bash
pkill apache
```
This command will terminate all processes containing the name apache.
Additionally, you can use the following options with pkill:
- `-u [username]`: kills processes owned by a specific user.
- `-t [terminal]`: kills processes attached to a specific terminal.
- `-l`: provides a detailed list of processes along with the PID.
Please use caution when using pkill, as it can affect multiple processes. For example, if you have processes named apache and apache-helper, running `pkill apache` will terminate both. Always make sure to target the exact process name to avoid unwanted interruptions.
### How to Kill a Process Using the killall Command
The killall command is similar to pkill but has a distinct method of process termination in Linux.
While pkill is effective for killing processes based on a partial match of the process name, the killall command requires the exact matching name of the process. This makes killall more specific and less likely to terminate unintended processes.
To use this command, type `killall [processname]`. For example:
```bash
killall apache
```
This will terminate all processes with the name apache. It is especially useful for killing background processes simultaneously.
The killall command can be customized with timestamp options, which include:
- `-o [time]`: only kills processes older than the specified time.
- `-y [time]`: only kills processes newer than the specified time.
The `[time]` specification in these options is generally given in the format of s for seconds, m for minutes, h for hours, d for days, w for weeks, M for months and y for years.
For example, this command terminates all currently running processes named chrome that have been running for more than 30 minutes:
```bash
killall -o 30m chrome
```
Conclusion
In this guide, we have explored several methods to kill a process in Linux using a shell script. We have covered different signals and the pkill, killall and kill commands. These techniques are crucial for facilitating a safe shutdown in Linux and preventing system instability.
Mastering these skills is vital for managing background processes safely and precisely, which is key to effective task management in Linux.
Come chat with us and we will get back to you as soon as possible!
Contact SupportAll the information has been carefully documented and made available in our most recent YouTube tutorial. You can watch it below.
The SkinRestorer plugin can be very useful if you are looking to use your server in non-premium mode. In this case, when configuring it as "online-mode:f
The PHP.ini file is a configuration file that contains your web server's PHP settings. Although it comes preconfigured, you may need to change the default PHP