Introduction

If a Linux process becomes unresponsive or is consuming too many resources, you may need to kill it.

Most processes have their own methods of shutting down. Unfortunately, processes can malfunction and not allow themselves to be shut down. If a background process running is unresponsive, it becomes necessary to use a command to kill it.

Here’s a complete guide on how to kill a Linux process using the command line.

how-to-kill-a-process-in-linux

What Processes Can You Kill in Linux?

Before killing or terminating a process, you need to consider permissions.

A root user can kill all processes. You can either add sudo before a command to run it as root, or obtain a root shell with su. Then execute the command.

Killing a process sends a termination message to the given process. There are multiple types of termination messages including:

  • SIGKILL – SIGKILL is the ultimate way of killing a process. It will always kill a process and will kill the process abruptly, generating a fatal error. SIGKILL should always work; if it does not work, the operating system has failed.
  • SIGTERM –SIGTERM attempts to kill a process, but unlike SIGKILL it may be blocked or otherwise handled. It can be considered a gentler way of attempting to terminate a process.

For most purposes, SIGKILL will be the fastest and most effective method to terminate the process.

Step 1: View Running Linux Processes

The top command is the easiest way to get a complete overview of the processes currently being run.

To view a list of all currently running processes, use the command:

top

The top command will reveal process IDs and users, in addition to the amount of memory and CPU power each process is using.

Top command kill process in Linux

To kill processes directly from the “top” interface,  press “k” and enter the process ID.

 

To exit the “top” interface, press the “q” button.

Step 2: Locate the Process to Kill

Before you can kill a process, you need to find it. There are multiple ways you can search for a process in Linux. Processes can either be located by a process name (or a partial process name) or a process ID (also known as a “pid”).

Locate a Process with ps Command

The ps command displays similar information to top, though it will not be in the form of an interface. Instead, the ps command provides a complete listing of running processes, formatted based on the tags you add.

ps <options>

The most common options to add to this is “-aux”:

  • -a. View processes of all users rather than just the current user.
  • -u. Provide detailed information about each of the processes
  • -x. Include processes that are controlled not by users but by daemons.

For example, the command ps -aux will return a detailed process list of all processes.

ps aux kill process in linux

Finding the PID with pgrep or pidof

The Linux command pgrep is a more complex way of finding a process. This command will return processes based on specific selection criteria, which is known as the pattern. The pattern is a regular expression, such as “a*“, where * would be a wildcard.

pgrep <options> <pattern>

Here are the options that can be used with this command:

  • -l. List both the process names and the PIDs.
  • -n. Return the process that is newest.
  • -o. Return the process that is oldest.
  • -u. Only find processes that belong to a specific user.
  • -x. Only find processes that exactly match the given pattern.

The command pgrep -u root displays all processes owned by root. The command pgrep -u root 'a*' returns processes owned by root that start with the letter “a”.

The pidof command is used to find the ID of a process, provided that you know the name of the process.

pidof <options> <program>

A few options can be included, such as:

  • -c. Only return PIDs within a single root directory.
  • -o. Omit certain PIDs (include the processes to omit after the flag).
  • -s. Only return a single PID.
  • -x. Also returns PIDs of shells that are running scripts.

Step 3: Use Kill Command Options to Terminate a Process

There are a few different methods of killing a process in Linux, depending on whether you know the name of the process running, the pid of the process, or just how long the process has been running.

killall Command

The killall command is used to kill processes by name. By default, it will send a SIGTERM signal. The killall command can kill multiple processes with a single command.

killall <process>

Several options can be used with the “killall” command:

  • -e. Find an exact match for the process name.
  • -I. Ignore case when trying to find the process name.
  • -i. Ask for additional confirmation when killing the process.
  • -u. Only kill processes owned by a specific user.
  • -v. Report back on whether the process has been successfully killed.

In addition to killing processes based on name, the killall command can also be used to kill based on the age of the process, using the following commands:

  • -o. Use this flag with a duration to kill all processes that have been running more than that amount of time.
  • -y. Use this flag with a duration to kill all processes that have been running less than that amount of time.

The killall -o 15m command will kill all processes that are older than 15 minutes, while the killall -y 15m command will kill all processes that are less than 15 minutes.

pkill Command

The pkill command is similar to the pgrep command, in that it will kill a process based on the process name, in addition to other qualifying factors. By default, pkill will send the SIGTERM signal.

pkill <options> <pattern>

pkill options include:

  • -n. Only kill the newest of the processes that are discovered.
  • -o. Only kill the oldest of the processes that are discovered.
  • -u. Only kill the processes that are owned by the selected user.
  • -x. Only kill the processes that match the pattern exactly.
  • -signal. Send a specific signal to the process, rather than SIGTERM.

kill Command

If you know a process ID, you can kill it with the command:

kill <processID>

The kill command will kill a single process at a time with the given process ID. It will send a SIGTERM signal indicating to a process to stop. It waits for the program to run its shutdown routine.

The -signal command can be used to specify a signal that isn’t SIGTERM.

kill-9 Command

kill -9 is a useful command when you need to shut down an unresponsive service. Run it similarly as a regular kill command:

kill -9 <processID>

Or

kill -SIGKILL <processID>

The kill -9 command sends a SIGKILL signal indicating to a service to shut down immediately. An unresponsive program will ignore a kill command, but it will shut down whenever a kill -9 command is issued. Use this command with caution. It bypasses the standard shutdown routine so any unsaved data will be lost.

Your operating system is not running properly if a SIGKILL signal does not shut down a service.

top Command

The top command provides an interface through which a user can navigate through currently running processes.

top

To kill a specific process, insert k from the top interface and then enter in the desired process ID.

kill-top-command-linux

xkill command

The xkill command is a special type of command that closes a given server’s connection to clients.

xkill <resource>

If a server has opened a number of unwanted processes, xkill will abort these processes.

If xkill is run without specifying a resource, then an interface will open up that lets the user select a window to close.

Key Takeaways on Terminating a Linux Process

  • When a process cannot be closed any other way, it can be manually killed via command line.
  • To kill a process in Linux, you must first find the process. You can use the top, ps, pidof or pgrep commands.
  • Once you have found the process you want to kill, you can kill it with the killall,pkill,kill,xkill or top commands.
  • When killing a process, you can send a termination signal of SIGHUP, SIGKILL, or SIGTERM.
  • You need to have permission to kill a process, which you can gain through the use of the sudo command.

Conclusion

In this article, we covered several ways to kill processes in Linux. It is critical to learn and understand these Linux termination commands for management and systems administrator.