Home Learn Linux 5 quick ways to kill a process in Linux

5 quick ways to kill a process in Linux

by Humphrey
ways to kill a process linux

Linux Operating System provides several ways of terminating a Linux operating system process. The obvious way to stop a program is by clicking the X button in the top left or right corner. But there are myriad other ways to kill a process; we are here to discuss the other efficient ways.

Ways to kill a process in Linux

Control sequence

The second obvious way to kill a process that has just been launched is to type Ctrl-C in the command line. The Ctrl-C control sequence is generally safe to use even though some data loss is possible.

The Ctrl-Z sequence will suspend a running process. It sends the SIGTSP signal to a process whose default action is to suspend the process.

kill commands

The Linux operating system comes with the kill command that you can use to terminate a process when they become unresponsive or is consuming too many resources. A process in Linux is an instance of a program.

A unique PID is automatically generated for that process when a program starts. The init process is the first process that starts when a Linux system is booted and assigned a value of ‘1’.

Init is the master process and cannot be killed through the kill command. The kill command on the init process is merely a request for the system to shut down.

This guide will take you through the steps and several ways to terminate a running process using the Linux terminal.

The root user has all the permissions required to kill all processes. You can obtain a root shell with the command su or add sudo before the command and run it.

The default signal sent by the kill command

Killing a process will send a termination message to the specified process. The two main types of termination messages include:

• SIGTERM – The kill command will send a SIGTERM signal by default. It should allow the process to terminate through its normal shutdown procedures. It is a gentler way of attempting to terminate a process and may be blocked.

• SIGKILL – It will always kill a process abruptly. If SIGKILL does not work, the operating system has failed. It is the fastest and most effective method to kill a process.

List running processes

The top command, ps command, and the pidof Command are the easiest ways to view running processes in a Linux system.

Syntax:

ps -e | grep name-of-process

The screen-shot below shows how to use the ps command to check if MySQL is running on the system. It gives us the PID of the MySQL daemon, which is1162.

Image showing how to use Grep MySQL

Image showing how to use Grep MySQL

Common options to add to the ps command

• -a: view processes from all users.
• -u: view detailed information about each process.
• -x: view all processes that are controlled by daemons.

ps -e
options to add to the ps command

options to add to the ps command

Syntax:

top

The top command will show you the PID, User, name of the resource, and how many system resources each process uses. These are useful if you decide to terminate any of the processes.

The top Command Results

The top Command Results

 

Using the pgrep command is also very efficient in obtaining the corresponding process ID.

Using the pgrep Command

Using the pgrep Command

Common options to add to the pgrep Command.

• -l: list the process names and the PIDs.
• -n: Return the newest process.
• -o: Return the oldest process.
• -u: Find processes that belong to a specific user.
• -x: Find a process that exactly matches a given pattern.

The pidof command is used to find the IP of a process if you know the process’s name.

Syntax:

pidof <options> <program>

Options:

• -c: Returns PIDs within a single root directory.
• -o: It will omit certain PIDs.
• -s: Return a single PID.
• -x: Returns PIDs of shells that are running scripts.

You can kill a process in Linux through different methods depending on whether you know the PID of the process, the name of the process, or the time the process has been running.

killall command

The killall command is used to kill processes by name. It can kill multiple processes with a single command, and it sends a SIGTERM signal by default.

Syntax:

killall <process>

Options available with the killall command:

• -e: Return the exact match for the process name.
• -I: The command will ignore the case when finding the process name.
• -i: It will ask for additional information when killing a process by name.
• -u: It will kill processes owned by a specified user of the system.
• -v: It will give feedback on whether the kill process has been executed successfully.

The killall command can also terminate processes in a Linux system based on the processes’ run times.

• -o: This option will kill all processes for more than the specified amount of time.
• -y: This option will kill all processes running less than the specified amount of time.

Example:

• killall -o 20m: The command will kill all processes that are older than 20 minutes.
• Killall -y 20m: The command will kill all processes running for less than 15 minutes.

kill command

The kill command will kill a process if you know the process IP.

Syntax:

kill <processID>

The kill command kills a single process with the given process ID. The Command will send a SIGTERM signal to stop and wait for the process to undergo its routine shutdown procedures.

Kill -9 command

The kill -9 command is used to terminate unresponsive processes or services.

Syntax:

kill -9 <processID>

or

kill -SIGKILL <processID>

The kill -9 command will send a SIGKILL signal to shut down a process immediately. The kill -9 command will bypass the standard shutdown routine, and any unsaved data will be lost.

Unresponsive programs will ignore the kill command but will terminate whenever the kill -9 command is issued.

Other commonly used kill options include:

• -1 (HUP): This is the “hang up” option and is generally very safe.
• -2 (SIGINT): This is the same signal as the Ctrl-C and is considered a safe alternative to kill a program.
• -15 (-TERM): this is the default terminate signal and is generally very safe.

You can get a complete list of all the signals available using the kill -l command.

Complete list of all the signals available using the kill -l command

Complete list of all the signals available using the kill -l command

pkill command

The pkill Command will kill a process when you know the process name. By default, pkill will send the SIGTERM signal to terminate the process.

Syntax:

pkill <options> <pattern>

pkill options:

• -n: This Command will kill the newest processes.
• -o: This Command will kill the oldest processes.
• -u: This Command will kill processes based on the selected user.
• -x: This Command will kill processes that match a pattern.
• -signal: Sends a specified signal other than the default signal SIGTERM.

xkill

The xkill Command will close a server’s connection to clients.

xkill <resource>

The xkill Command will terminate unwanted server processes.

top command

The top command provides an interface through which a user can specify the termination process.

Syntax:

top

To kill a process by PID, insert k from the interface and then enter a specified process ID.

Recap

• You need to have sudo permissions to kill a process through the command line.

• Whenever you generate a signal using the kill command, the Linux system interrupts the target process’s normal flow of execution.

• To find a running process, you can use the ps, top, pgrep, or pidof commands.

• You can kill a process by name or process Id using the kilall, pkill, kill, xkill, or top commands.

• A kill process will send a termination signal of SIGTERM, SIGHUP, OR SIGKILL.

• You can use the System Monitor to kill the Linux process. It can be found in the system tools, and it provides several options like the top process, end process, or kill the process.

• The kill command and its variants are pretty straightforward to understand and use.

In this guide, we highlighted various ways to kill processes in Linux. There’s a slight learning curve in terms of the list of signal options it offers, which is essential for proper Linux administration. If you want to learn more, I suggest looking at their respective man pages.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.