How to kill a process in Linux

When an application hangs, consumes too many resources, or needs a clean restart, you may have to terminate (or ‘kill’) its process. This guide shows safe, step-by-step ways to find and end Linux processes using standard tools, whether you’re working on Ubuntu, Debian, CentOS/Alma Linux, or other distributions. It applies equally to servers running on the Kamatera Cloud Platform, via SSH or your Kamatera console.

Quick signal cheat sheet

SIGTERM (15): ask the process to exit gracefully (default)
SIGKILL (9): force stop (last resort)

Step-by-step guide

1. Connect and prepare

To begin managing your server, you first need to establish a secure connection to it. This is done using SSH (Secure Shell), which allows you to remotely log in and run commands on the server.

Command: ssh user@your.server.ip 

ssh: The command used to start an SSH session.

User: Replace this with your server’s username (for example, root or another admin account). 

Your.server.ip: Replace this with the public IP address or hostname of your server.

When you run this command, you will be prompted to enter the account’s password (unless you have configured SSH keys). Once authenticated, you will gain terminal access to your server, and from there you can run the necessary setup or management commands.

Note: If you’re already logged in as the root user (likely on Kamatera), you don’t need sudo.

How to kill a process in Linux

2. Viewing running processes

To monitor and understand what applications or services are currently active on your server, you can list the running processes. 

Command: ps

It displays processes running under the current shell (the session you are logged into). Useful for quickly checking what you personally started in this session.

How to kill a process in Linux

Command: ps aux

a: Shows processes for all users, not just your own.

u: Provides detailed information such as the user running the process, CPU usage, and memory usage.

x: Includes processes that don’t have a controlling terminal (for example, background services like web servers or databases).

This command gives a comprehensive overview of all running processes on the system, making it easier to identify resource-heavy tasks or troubleshoot issues.

How to kill a process in Linux

3. Identifying processes

When managing a server, it’s important to identify which processes are running, their process IDs (PIDs), and what resources they are using. Below are different ways to find them: 

  • By PID (Process ID):
Command: ps aux | grep <name>

It searches for a process by name and shows detailed information, including its PID.

Example: ps aux | grep sshd. 

This will display all running sshd processes as shown in the screen below.

How to kill a process in Linux

  • By process name (more targeted):
Command: pgrep -fl <process_name>

It lists processes by name along with their PIDs and easier to use than ps aux | grep since it directly searches processes.

Example: pgrep –fl sshd

This will show the PID(s) and command line for all running sshd processes.

How to kill a process in Linux

  • By resource usage (when you’re unsure):
       Command: top   

An interactive tool showing CPU, memory, and other resource usage. This command is useful for spotting processes that are consuming a lot of resources.  

Press P or M to sort by CPU or memory

Press k: kill a process (enter PID and signal when prompted)

Press h: show help menu

Press q: That immediately quits the top session. 

How to kill a process in Linux

Command: htop 

This command gives a more user-friendly, interactive version of top. It displays processes in a tree view, making it easier to understand parent-child relationships. Here, you can scroll, search, and kill processes directly from the interface as shown in the screen below.

Note: htop interface is shown only if it is already installed.

How to kill a process in Linux

  • By network port (for services)

Sometimes you need to check which service is listening on a specific port.

Command: sudo ss -lptn | grep :<port>

Example: sudo ss -lptn | grep :22 

This shows which process is listening on port 22 (typically SSH). 

How to kill a process in Linux

Command: sudo lsof -i :<port> -sTCP:LISTEN

Example: sudo lsof -i :22 -sTCP:LISTEN 

This lists the process (with PID and command) that is listening on port 22

How to kill a process in Linux

4. Graceful termination (SIGTERM)

When you want to stop a process, it is best to begin with a graceful termination. This uses the SIGTERM (signal 15), which politely asks the process to shut down. Unlike a forced kill, SIGTERM allows the application to clean up resources, close files, or save data before exiting. 

Note: Always try SIGTERM first before moving to stronger signals (like SIGKILL), as it minimizes the risk of data loss or corruption. 

Command: sudo kill <PID>

Here, replace <PID> with the Process ID of the application you want to stop.

(or) explicitly (with signal 15):

Command: sudo kill -15 <PID>

Here, replace <PID> with the Process ID of the application you want to stop.

How to kill a process in Linux

5. Kill by name (when multiple instances)

Sometimes, you may need to stop all processes running under the same name instead of identifying and terminating them one by one. Linux provides multiple ways to do this: 

Here, we used the process name ‘yes’. It is actually a standard Linux/Unix command-line utility. When you run ‘yes’ in a terminal, it outputs y (or any string you provide) repeatedly until stopped. Because it runs continuously, it’s often used as a simple example process in tutorials about terminating processes. 

  • Using pkill (pattern-based):
Command: sudo pkill -TERM <process_name>

Example: sudo pkill -TERM yes

This command sends a termination signal (SIGTERM) to all processes whose names match the given pattern.
Note: Because pkill uses pattern matching, it may terminate unintended processes if the name is too broad. 

  • Using killall (exact match):
Command: sudo killall -TERM <process_name>

Example: sudo killall -TERM yes

This command stops all processes with the exact specified name. It is generally safer than pkill when you know the exact process name.

  • Using kill with PID (targeted):
Command: sudo kill -TERM <PID>

Example: sudo kill -TERM 28353

This method requires the specific PID (Process ID) and affects only that single process. 

Note: Use kill with a PID when possible for precision, and use killall if you are sure of the process name. Use pkill cautiously due to its pattern-matching behavior.

How to kill a process in Linux

6. Force kill (SIGKILL) (use as last resort)

If a process does not respond to a graceful termination (SIGTERM), you can use SIGKILL (signal 9) to immediately force it to stop. Unlike SIGTERM, this method does not allow the process to perform cleanup (such as saving data or closing connections), so it should only be used when other methods fail.  

  • Kill by PID (most precise): 
Command: sudo kill -9 <PID>

Example: sudo kill -9 28374

This terminates the specific process with the given PID instantly. 

  • Kill by process name (pkill):
Command: sudo pkill -9 <process_name>

Example: sudo pkill -9 yes

This command terminates all processes matching the given name. 

  • Kill by process name (killall)
Command: sudo killall -9 <process_name>

Example: sudo killall -9 yes

This command also terminates all processes matching the specified name. 

Note: Use SIGKILL only, when necessary, as it does not allow the process to clean up resources or save work. 

How to kill a process in Linux

7. Interactive termination using top or htop

You can terminate processes interactively using system monitoring tools like top or htop, which allow you to view running processes and send signals directly. 

  • Steps in top:

Open the terminal and run the below command

Command: top

This displays a live list of running processes. 

Press k to initiate a process kill and enter the PID (Process ID) of the process you want to terminate as shown in the screen below.

Example: In the example below, we want to kill the process named ‘yes’, which has a PID of ‘28403’. 

How to kill a process in Linux

Enter the signal number to send (The default signal is 15 (SIGTERM), which requests graceful termination. Use 9 (SIGKILL) only if the process does not respond). Press Enter to confirm and send the signal. 

How to kill a process in Linux

From the screen below, you can verify that the ‘yes’ process has been terminated. 

How to kill a process in Linux

When you exit top by pressing q, you will see a line like: [2]- Terminated yes > /dev/null indicating that the process has terminated.

How to kill a process in Linux

  • Steps in htop:

Open the terminal and run: 

Command: htop

This displays a colorful, interactive list of running processes. 

Use the arrow keys to navigate and select the process you want to terminate. 

Example: Here, the process name ‘yes’ is selected with PID 28448.

How to kill a process in Linux

Press F9 to bring up the kill menu. Now, select the signal you want to send (Default is 15 (SIGTERM) for graceful termination. Use 9 (SIGKILL) if the process does not respond). Press Enter to confirm and send the signal.

How to kill a process in Linux

The process will be terminated, and the list will update automatically as shown in the screen below.

How to kill a process in Linux

8. Handle zombies & orphaned processes

A zombie process is a process that has completed execution but still remains in the process table because its parent has not yet read its exit status. Zombie processes cannot be killed directly, as they are already terminated. 

How to identify:

  • Run the below command to list all processes.  
Command: ps

Once the processes are listed, you can see the number of zombie processes (for example, 1 zombie). Zombie processes are marked with a Z in the process status column, as shown in the screen below. 

How to kill a process in Linux

How to clean up:

  • Send a SIGCHLD signal to the parent process to encourage it to reap the zombie child.
Command: kill -s SIGCHLD <parent_PID>

To find the Parent Process ID (PID), use the following commands:

Command: ps aux | grep ‘zombie’

Command: top

Note: Zombie processes cannot be killed directly. Prompting the parent or terminating it (if safe) allows the system to clean up the zombie process. 

9. Managing service-managed processes with systemd in Kamatera

Many processes on Kamatera servers are managed by systemd, which handles starting, stopping, and monitoring services. You can use systemd commands to stop, check, or forcibly terminate services. 

Note: If you’re already logged in as the root user (likely on Kamatera), you don’t need sudo.

  • To know the services that are running, active, inactive, failed, run the below command:
               Command: systemctl list-units --type=service

How to kill a process in Linux

  • Stop a service:
Command: systemctl stop <service>

Example: systemctl stop irqbalance

This stops the specified service and prevents it from running until manually restarted.

Now, check the service status by running the below command.

Command: systemctl status <service>

Example: systemctl status irqbalance

From the screen below you can see the status is Inactive. It also displays logs and any active processes for the service.

How to kill a process in Linux

  • Force kill a service (if unresponsive)

 

Command: systemctl kill <service>

(or)

Command: systemctl kill --signal=SIGKILL <service>

How to kill a process in Linux

  • If you want to make sure it doesn’t start again after reboot, you should also disable it:
Command: systemctl disable <service>

How to kill a process in Linux

10. For Kamatera users

  • Ensure SSH reliability:

When running long processes over SSH, they may be terminated if the connection drops. To prevent this, you can use nohup to run commands immune to hangups:

Command: nohup <command> &

Example: nohup top &

How to kill a process in Linux

The trailing & makes the process run in the background, even if you log out (hangup), the process keeps running.

  • screen to create a detachable session that continues running even if you disconnect.

 

  • Watch automatic restarts:

Some services may automatically respawn, which can interfere with maintenance or stopping processes. To prevent this: 

Command: sudo systemctl disable --now <service>

Example: sudo systemctl disable –now irqbalance 

This stops the service immediately and disables automatic restart on boot. 

How to kill a process in Linux

  • Manage clustered or background tools:

For services managed by clustering or process managers (e.g., Docker containers, PM2 for Node.js apps), ensure you handle restarts according to their management tools to avoid unexpected service behavior.

Note: Using these methods helps maintain control over running processes and prevents accidental terminations or restarts during server maintenance.

  • Killing a process with xkill (desktop environments only)

On Linux systems with a graphical desktop environment (less common on Kamatera servers but useful on local workstations), you can use xkill to forcefully terminate unresponsive windows. 

a. Install xkill by running the below command:

Command: sudo apt install x11-utils

b. Run xkill

Command: xkill

Your mouse pointer will change to a cross or skull icon.

c. Terminate the window:

Click on the window you want to close. The system will immediately kill the process associated with that window.

Note: xkill forcefully terminates the process behind a window (similar to kill -9). Only use it when the normal close options do not work, as it does not allow the process to perform any cleanup.

And that’s it! You have now learned safe ways to find and end Linux processes on your Kamatera cloud server.

Have additional questions? Search below: