How to Kill a Process in Linux

Sometimes a Linux process consumes too many resources or becomes unresponsive. When this happens, you need to kill the process. Note that you can only kill your own processes. You cannot kill other users’ processes or system-level processes. Only the root user can kill system-level processes or processes started by other users.

Note: We’re using Zorin OS 16, an Ubuntu-based distro, for this guide. Linux commands featured in this article are distro-independent, meaning they will work on any Linux distro.

Commands used to find a process

Before killing a process, you must first find the information required to kill the process. This is typically the process name or the process ID (PID). You must have one or the other to kill a process. Fortunately, you can quickly get this information using one of the many built-in command-line utilities. The two most commonly used ones, however, are top and ps.

top

The top (table of processes) command provides a real-time view of running processes and resource utilization like CPU and memory usage. It also provides PIDs with their corresponding process/program names. The top command helps identify processes over-utilizing CPU and/or memory usage. The top command’s screen refresh is set to 3.0 seconds. Press the q key to exit top.

The top command displays tasks and system status.
The top command displays tasks and system status.

top Command Syntax and Keyboard Shortcuts

# top [options]

While top is running, two keyboard shortcuts greatly assist in finding troublesome processes.

  • Press <Shift><M> and <Shift><P> (consecutively) to sort all running processes by memory usage.
  • Press <Shift><P> to sort processes by CPU usage.

You can also kill a process using the top command. We’ll explain how to do this later in this article.

ps

The ps (process status) command lists processes much like the top command, but not in real-time. Also, ps lists only:

  • PID – the process ID.
  • TTY – the terminal type that the user is logged into.
  • TIME – the amount of CPU in minutes and seconds that the process runs.
  • CMD – the name of the command that launched the process.
ps command example.
ps command example.

ps Command Syntax and Options

# ps [options]

The ps command reports a snapshot of the current processes.

Since entering the ps command with no options show only the processes running in the current shell, the shell itself, and the processes that run in the shell when the command was invoked, options are needed with the ps command when used to find processes to kill. Specifically, these options are:

  • -a  — show processes for all users.
  • -u — display the process’s user/owner.
  • -x — also show processes not attached to a terminal.
ps -aux command output.
ps -aux command output.

grep Command Syntax

# grep [options] pattern <files>

grep searches for PATTERNS in each FILE. PATTERNS is one or more patterns separated by newline characters, and grep prints each line that matches a pattern. Typically PATTERNS should be quoted when grep is used in a shell command.

ps Command Piped with grep

The ps command is most useful when ‘piped’ with the grep command. Piping allows you to send the output of one command to another command. The “|” symbol, also known as the vertical bar, is used to pipe the commands. The grep command allows you to find matching patterns. So, if we wanted to find all the processes used by thunderbird, we would enter:

# ps -aux | grep thunderbird

Above we are using ps with the -aux options to grep the term thunderbird.

grep searches for PATTERNS in each FILE. PATTERNS is one or more patterns separated by newline characters, and grep prints each line that matches a pattern. Typically PATTERNS should be quoted when grep is used in a shell command.

The output from this command shows all processes started by the thunderbird application (note that the search pattern (thunderbird) used with grep is case-sensitive).

The output of ps -aux | grep thunderbird.
The output of ps -aux | grep thunderbird.

This provides us with the relevant PIDs associated with the thunderbird process, so we can use the commands featured in the next section to kill all thunderbird-associated processes.

Commands used to kill a process

The top, kill, and killall commands, when used in tandem with the top or ps commands, allow Linux users to pinpoint and kill troublesome processes.

top

As mentioned earlier, you can use the top (table of processes) command to kill a process.

Kill a Process With the top Command

Pressing the k key while top is running will allow you to kill a process by entering the process’s PID.

top Command Real Life Scenario

Let’s suppose our system is running slowly, and we want to find out what’s causing it and then kill the offending process.

  1. Launch the terminal.
  2. Start top.
    # top

    Launch top.
    Launch top.

  3. Press <Shift><M> and <Shift><P> to find out what processes are using the most system memory.

    Processes sorted by memory with top.
    Processes sorted by memory with top.

  4. Next, press <Shift><P> to determine what processes are stressing the CPU.
    Processes sorted by CPU usage with top.
    Processes sorted by CPU usage with top.

    In the above two screenshots, we can see that one process is hogging both the system’s CPU and memory (this is often the case). That process is thunderbird. Let’s kill it.

    From the top screen, we find the corresponding PID to the offending program (thunderbird). In this case, it’s 1238830.

  5. Press k and enter the PID (1238830). Press <Enter>.

    Use top to kill Thunderbird.
    Use top to kill Thunderbird.

  6. At the Send pid 1238830 signal [15/sigterm] press <Enter> to accept the default.

The thunderbird process has been successfully killed.

kill

The kill command sends a signal to a specified process (or process group) to act according to the signal sent. The two most common signals are:

  • -9 (SIGKILL)      — Kill a process abruptly. Any unsaved data associated with the process will be lost.
  • -15 (SIGTERM) — Gracefully stop a process.

If no signal is specified, it defaults to -15 (SIGTERM).

kill Command Syntax and Options

# kill options <PID>

kill Command Real Life Scenario

Let’s suppose our system is running slowly, and we suspect that the firefox is the culprit. First, let’s verify that this is the problem with the ps -aux command piped with the grep command.

# ps -aux | grep firefox

Once verified and the PID identified (1258549), let’s use the kill command to kill it.

# kill 1258549

Next, we verify that the process has been killed.

# ps -aux | grep firefox

We can now see that no firefox-associated processes are running. The only process associated with firefox displayed

mtravis+ 1259037 0.0 0.0 9040 720 pts/0 S+ 15:05 0:00 grep –color=auto firefox

is actually the ps -aux | grep firefox command we just ran.

Using the kill command to end the firefox process.
Using the kill command to end the firefox process.

killall

The killall command is much like the kill command, but it allows you to kill processes by process name instead of PID. The same signals used with the kill command work with the killall command. Like the ps command, the process name is case-sensitive when specifying with the killall command.

killall Command Syntax and Options

# killall [options] <process name>

killall Command Real Life Scenario

Let’s suppose our system is running slowly, and we suspect that our ssh server (sshd) is the culprit.

Let’s first verify that this is indeed the problem with the ps -aux command piped with the grep command.

# ps -aux | grep sshd

Once verified, let’s use the killall command to kill it.

# killall sshd
# ps -aux | grep sshd

We can now see that no sshd-associated processes are running.

Using the killall command to end the sshd process(es).
Using the killall command to end the sshd process(es).

Commands Used in This Guide

  • ps (process status)list processes.
  • grep – searches for processes in a file.
  • top (table of processes) – real-time view of running processes.
  • kill – kills processes by PID.
  • killall – kills processes by process name.

Wrapping Up

There are many options to find and delete processes. In my experience, the quickest and easiest method to kill a process is to first find the offending process using the top command, and then kill it with the killall command. There’s no need to memorize PIDs or chase down processes with the ps command.

Leave a Comment