Efficient Ways for Terminating Processes in the Linux Terminal

Efficient Ways for Terminating Processes in the Linux Terminal

Learn how to efficiently terminate processes from your Linux or macOS command line with our comprehensive guide Master the kill, pkill, and killall commands to swiftly eliminate any problematic process

Some Noticeable Information

Killing a process simply means forcing it to quit, and it can be necessary when a process is unresponsive or misbehaving.

Linux and macOS provide commands such as kill, pkill, and killall to terminate processes by their PID or name. It is crucial to exercise caution while killing processes to ensure that you are terminating the intended one and to prevent any unintended consequences.

What is a Process?

Sometimes, the only solution to eliminate a process is by killing it. Although the term "killing" may sound severe, it simply refers to forcibly quitting the process. Below are the steps to accomplish this using the command line in Linux or macOS.

Running programs like your web browser, background processes associated with your desktop environment, and Linux system services are all processes.

You can lump processes into two groups:

Foreground processes are processes initiated or initiated by users, whether they are running in a terminal window or as a graphical application.

In contrast, background processes refer to all processes that start automatically and do not require any interaction with users. These processes do not expect input or provide output to users. Examples of background processes include services and daemons.

Foreground processes represent the theater staff and actors, while background processes make up the backstage "behind the scenes" team.

Misbehaving or malfunctioning processes can excessively consume CPU resources, occupy significant RAM, or get stuck in a repetitive computational loop and fail to respond. Graphical applications may become unresponsive to mouse clicks, and terminal applications could fail to return to the command prompt.

What Does Killing a Process Do?

"Killing" a process just means "forcing the process to quit." This may be necessary if the process is refusing to respond.

Linux offers several commands, namely kill, pkill, and killall, that enable you to terminate processes efficiently. These commands are compatible with all types of processes, whether they are graphical or command line-based, running in the foreground or background.

The kill Command

To terminate a process using the kill command, you must have the process ID (PID) of the specific process. The PID can be obtained by using the ps command.

To search for the PID of all processes, use the -e option with the ps command. It is recommended to pipe the output through less to manage the large amount of information. To do this, enter ps -e | less and press Enter to execute the command.

ps -e | less

Efficient Ways for Terminating Processes in the Linux Terminal

This will give you a process listing that looks similar to the below screenshot. You can search forward in less using the / key and you can search backward using the ? key.

Efficient Ways for Terminating Processes in the Linux Terminal

To home in on the process you're interested in, pipe the output from ps through grep and specify the name — or part of the name — of the process.

ps -e | grep shutter

Efficient Ways for Terminating Processes in the Linux Terminal

To terminate the shutter process that you have identified using the PID, pass the PID as a parameter to the kill command. Use the following command to terminate the shutter process:

kill 2099

Efficient Ways for Terminating Processes in the Linux Terminal

The kill command is a silent assassin — it does not give you any feedback if it was successful.

It also works just the same for killing processes on macOS.

The pkill Command

The pkill command offers the capability to terminate processes based on their name rather than their PID. Instead of specifying the process ID, you simply input a search term which pkill compares against the active process list. Any matching processes will be ended. Therefore, it is crucial to ensure the search term is accurately spelled in order to ensure successful termination.

Use the pgrep command as a precautionary measure before employing the pkill command. The pgrep command allows for a search term, displaying the PID of each matching process. This approach is secure as pgrep does not send kill signals to processes, preventing accidental termination. By verifying the search term with pgrep, you can ensure its accuracy before passing it to pkill. Both pkill and pgrep handle the search term similarly, to the extent that they share the same manual page.

To illustrate, let's assume there is a process containing the term "subq" in its name. To gain insight into this process, execute the ps -u dave | grep command. This will reveal that "subq" exclusively matches that particular process. This demonstration provides a comprehensive understanding of the process's complete name.

ps -u dave | grep subq

Efficient Ways for Terminating Processes in the Linux Terminal

Assuming the user hasn't performed this task before, and they only have knowledge that the process name includes the substring "subq," they employ the method of pgrep to confirm the presence of a single matching process. Subsequently, they utilize the obtained search term to execute pkill.

pkill subq

Efficient Ways for Terminating Processes in the Linux Terminal

To kill multiple processes simultaneously, utilize pkill. In this scenario, the user executes pgrep to determine the number of Chrome processes initiated. By implementing pkill, all Chrome processes are terminated. Subsequently, a check is performed using pgrep to confirm their successful removal.

pkill chrome

pgrep chrome

Efficient Ways for Terminating Processes in the Linux Terminal

To differentiate between multiple processes running with the same name and selectively terminate one without affecting the others, the -f (command line) option of pgrep can be utilized. This option enables the identification of individual processes based on their specific command lines. For instance, if there are two ping processes and you wish to terminate only one while keeping the other intact, you can employ their respective command lines to distinguish between them. It is important to enclose the command line parameter within quotation marks as illustrated in this example.

pgrep -f "ping 192.168.4.22"

pkill -f "ping 192.168.4.22"

Efficient Ways for Terminating Processes in the Linux Terminal

The killall Command

Caution: When using the killall command in Solaris and OpenIndiana operating systems, all processes belonging to you will be terminated. If you are logged in as root or have executed sudo killall, your computer will restart. This behavior has been verified while researching for this article, using the latest version of OpenIndiana Hipster 2018.10.

The killall command functions similarly to the pkill command, but with a distinct variation. Rather than specifying a search term, you must provide the precise name of the process.

You cannot provide a partial match to a process name; you must provide the entire process name, as shown:

killall shutt

killall shutter

Efficient Ways for Terminating Processes in the Linux Terminal

The -y (younger than) option allows you to kill processes that have been running for less than a specified period. The period is given in numbers followed by one of these units:

s (seconds)

m (minutes)

h (hours)

d (days)

w (weeks)

M (months, note, capital "M")

y (years)

To terminate a newly launched process named ana and retain any previous instances of ana running, you can utilize the following options with killall, provided you respond within two minutes:

killall -y 2m ana

Efficient Ways for Terminating Processes in the Linux Terminal

The -o option, also known as 'older than,' enables you to terminate processes that have been active for a duration surpassing a specified time period. By utilizing this command, you can effectively terminate all ssh connections that have been active for more than one day.

killall -o 1d sshd

Efficient Ways for Terminating Processes in the Linux Terminal

Can You Kill Any Process?

By utilizing these instructions, you will be able to accurately and safely identify and terminate malfunctioning processes. Nevertheless, it is crucial to exercise caution and avoid terminating processes that should not be terminated.

Always exercise caution when terminating a process. Firstly, ensure that the process you intend to terminate is indeed the one you want to end. Secondly, take the extra step of double-checking to ensure that the targeted process is the correct one. Only proceed with terminating the process once you are completely certain.

In the event that you accidentally terminate a process, it is not the end of the world. The most probable consequence is that certain functionalities may become unstable, requiring you to restart your PC. Additionally, there is a possibility of losing any unsaved work associated with the program connected to the terminated process.

Linux Commands

Files

tar · pv · cat · tac · chmod · grep · diff · sed · ar · man · pushd · popd · fsck · testdisk · seq · fd · pandoc · cd · $PATH · awk · join · jq · fold · uniq · journalctl · tail · stat · ls · fstab · echo · less · chgrp · chown · rev · look · strings · type · rename · zip · unzip · mount · umount · install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · vi · nano · mkdir · du · ln · patch · convert · rclone · shred · srm · scp · gzip · chattr · cut · find · umask · wc · tr

Processes

alias · screen · top · nice · renice · progress · strace · systemd · tmux · chsh · history · at · batch · free · which · dmesg · chfn · usermod · ps · chroot · xargs · tty · pinky · lsof · vmstat · timeout · wall · yes · kill · sleep · sudo · su · time · groupadd · usermod · groups · lshw · shutdown · reboot · halt · poweroff · passwd · lscpu · crontab · date · bg · fg · pidof · nohup · pmap

Networking

netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami · w · iptables · ssh-keygen · ufw · arping · firewalld

Editor's P/S

As a hard fan of Linux, I am always looking for new and efficient ways to terminate processes in the Linux terminal. This article provides a comprehensive guide to using the kill, pkill, and killall commands to swiftly eliminate any problematic process.

The kill command is the most basic way to terminate a process. It requires the process ID (PID) of the process you want to terminate. You can find the PID of a process by using the ps command. Once you have the PID, you can use the kill command to terminate the process.

The pkill command is similar to the kill command, but it allows you to terminate a process by its name. This can be useful if you don't know the PID of the process you want to terminate. The killall command is similar to the pkill command, but it terminates all processes that match a given name. This can be useful if you want to terminate all instances of a particular process.

I find these commands to be very useful for managing processes in the Linux terminal. They allow me to quickly and easily terminate any problematic processes. I highly recommend learning how to use these commands if you are a Linux user.