Use these Linux commands to force resignations to behave improperly
The simplest word to interpret is "process", which is any application or program running an instance on your system. You may run multiple applications at the same time, such as browsing, listening to music dedicated to the terminal, and so on. There are many users of applications running with these related background processes.
Each application or program runs on your system to create multiple processes associated with a single application. Sometimes this can be a problem and getting rid of these processes is the only option.
The Kill process is a useful choice for Linux to provide you with the option to stop an ongoing process, a foreground or background process. In this article, we will review the commands kill, pkill, and kill all to force any process on the system to exit.
Why kill a process?
Understanding the concept of killing a process is important prior to this tutorial. Homicide seems to be a very cruel way to express this concept, but what this metaphor means is to forcibly suspend the process.
Why stop or exit an ongoing process? When multiple processes are running in the background, or some of them may fail improperly, it may cause your system to fail. This delays your task crash process and may freeze your system.
Sometimes the process of abandoning all misconduct seems to be the only option for the system to return to normal. Linux can kill a process using pid or process name.
Use the pgrep command
Most Linux users are familiar with the grep command. The pgrep command of can be used for similar greps.
When the pgrep command is used, the command specified during the pid run is displayed. This command will prove very useful, using the pkill command as well.
General grammar:
pgrep [options] [pattern]
Important Select pgrep Command
option | describe |
-u | List of process IDs owned by a specific user |
-c | Matching process for calculating quantity |
-I | List process names only |
-a | Complete path name list for the process |
Let's demonstrate an example of using the pgrep command.
pgrep -u gaurav gnome
Here, we want to see all the "gaurav" owned by gnome users of the pids process. Option-u allows you to list the pids process by a specific user. In this case, user gaurav.
Output:
gaurav@ubuntu:~$ pgrep -u gaurav gnome 1752 1755 1909 1922 2021 2576 4279 gaurav@ubuntu:~$
As we move forward with this tutorial, the pgrep command will help us confirm if the process is killed or still running.
Now let's turn to the pkill command and execution.
Use the pkill command
You can use the pkill command in Linux to kill a process using the process name. Even if you don't know some pid processes, you can even kill that particular process using the pkill command.
You can specify the full or partial name of a process and use the pkill command at the same time. Even if you enter a partial name, the process pkill command will match the command entered in the matching name of all running processes.
Grammar:
pkill [options][process_name_pattern]
Example:
Let's display the top command using the process that is currently running. You can also use the ps command list process.
top
top - 14:24:02 up 3:12, 1 user, load average: 0.29, 0.48, 0.58 Tasks: 221 total, 1 running, 172 sleeping, 0 stopped, 1 zombie %Cpu(s): 5.6 us, 1.0 sy, 0.0 ni, 92.9 id, 0.4 wa, 0.0 hi, 0.1 si, 0.0 st KiB Mem : 3928240 total, 610456 free, 2233152 used, 1084632 buff/cache KiB Swap: 4083708 total, 3378884 free, 704824 used. 1187268 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 4077 gaurav 20 0 3312128 673480 118360 S 19.6 17.1 15:13.23 Web Content 3712 gaurav 20 0 3953008 453544 116476 S 4.0 11.5 9:28.39 MainThread 2010 gaurav 20 0 4084232 111096 45024 S 1.7 2.8 3:14.85 gnome-shell 1197 root 20 0 1039612 33704 22988 S 1.0 0.9 3:04.42 Xorg 1426 couchdb 20 0 3772396 16908 2520 S 0.7 0.4 1:50.83 beam.smp 3288 gaurav 20 0 722480 25048 18272 S 0.7 0.6 0:06.84 gnome-terminal- 3915 gaurav 20 0 2804900 231524 111228 S 0.7 5.9 0:54.42 Web Content 4146 gaurav 20 0 3017924 245304 120604 S 0.7 6.2 2:01.21 Web Content 4417 gaurav 20 0 2964208 234396 119160 S 0.7 6.0 0:59.90 Web Content 4860 gaurav 20 0 3066800 372920 132544 S 0.7 9.5 0:48.20 Web Content 16007 gaurav 20 0 41944 3780 3116 R 0.7 0.1 0:00.28 top
Use the top command to display multiple processes at the terminal. Let's try to show the process and a specific name. We will use the grep command to display a process name matching string "mongo".
top | grep -i mongo
Note: Here, I want to use the -I option to search case-insensitive.
The output of this command will display the name of the process matching "mongo"
1158 mongodb 20 0 288564 4848 1320 S 0.7 0.1 1:03.22 mongod 1158 mongodb 20 0 288564 4848 1320 S 1.0 0.1 1:03.25 mongod 1158 mongodb 20 0 288564 4848 1320 S 0.7 0.1 1:03.27 mongod 1158 mongodb 20 0 288564 4848 1320 S 0.7 0.1 1:03.29 mongod 1158 mongodb 20 0 288564 4848 1320 S 0.7 0.1 1:03.31 mongod 1158 mongodb 20 0 288564 4848 1320 S 0.7 0.1 1:03.33 mongod 1158 mongodb 20 0 288564 4848 1320 S 1.0 0.1 1:03.36 mongod 1158 mongodb 20 0 288564 4848 1320 S 0.7 0.1 1:03.38 mongod 1158 mongodb 20 0 288564 4848 1320 S 0.7 0.1 1:03.40 mongod 1158 mongodb 20 0 288564 4848 1320 S 1.0 0.1 1:03.43 mongod 1158 mongodb 20 0 288564 4848 1320 S 0.7 0.1 1:03.45 mongod 1158 mongodb 20 0 288564 4848 1320 S 1.0 0.1 1:03.48 mongod 1158 mongodb 20 0 288564 4848 1320 S 0.3 0.1 1:03.49 mongod 1158 mongodb 20 0 288564 4848 1320 S 1.0 0.1 1:03.52 mongod 1158 mongodb 20 0 288564 4848 1320 S 0.7 0.1 1:03.54 mongod 1158 mongodb 20 0 288564 4848 1320 S 1.0 0.1 1:03.57 mongod
Now we'll use the pkill command to kill the process named "mongo".
pkill mongo
This command will kill the mongo process. We can confirm whether we have been forced to exit processes that use the pgrep command to show that pid is running according to user-specified criteria.
gaurav@ubuntu:~$ pgrep mongo gaurav@ubuntu:~$
This command will return no value. This confirmation process "mongo" now uses the death pkill command.
Select common pkill commands
Using the pkill command we will need to choose the appropriate and easy pkill command to use.
option | describe |
-f | Match complete parameters including spaces, quotes, special characters |
-u | Notify that the pkill procedure matching process is performed by the specified user |
-1 | Reload process |
-9 | The process of death |
-15 | Abort process gracefully |
Let's look at an example of the pkill command using -f selection.
There are two commands executing the terminal, as shown below.
ping bbc.com ping youtube.com
Initiated process Ping command. Now, suppose we want only one process to terminate "ping youtube.com" and then we have to kill the process with the -f selection and pkill command with a specific name from the process name including spaces and quotes.
Command:
gaurav@ubuntu:~$ pkill -f "ping youtube.com" gaurav@ubuntu:~$
Result:
gaurav@ubuntu:~$ ping youtube.com PING youtube.com (142.250.67.206) 56(84) bytes of data. 64 bytes from bom12s08-in-f14.1e100.net (142.250.67.206): icmp_seq=1 ttl=117 time=30.9 ms 64 bytes from bom12s08-in-f14.1e100.net (142.250.67.206): icmp_seq=2 ttl=117 time=121 ms 64 bytes from bom12s08-in-f14.1e100.net (142.250.67.206): icmp_seq=206 ttl=117 time=86.5 ms 64 bytes from bom12s08-in-f14.1e100.net (142.250.67.206): icmp_seq=207 ttl=117 time=105 ms Terminated gaurav@ubuntu:~$
Here, "ping youtube.com" process is now dead, "ping bbc.com "is still running on the terminal.
In this case, if we use the pkill ping command, it will kill the ping process, which is not desirable.
Signal uses pkill command
The pkill forces quit smoking by sending specific signals. There are three possible signaling pkill commands that can be sent to a process based on user-supplied commands.
Below are the available signals in the list.
signal | Standard |
1(HUP) | Reload the specified procedure |
9(KILL) | Kill the specified procedure |
15(TERM) | Gently stop or abort the specified process |
For this tutorial, we will rely heavily on KILL signals. Let's get a better understanding of it through some examples.
Use the pid of the pgrep command to match the name of the apache.
gaurav@ubuntu:~$ pgrep apache 1218 10402 10403 gaurav@ubuntu:~$
pkill -KIll apache
Or you can use commands such as numbers. 1, 9, 15
pkill -9 apache
The commands shown above all killed the apache process. Confirmed pgrep command again.
gaurav@ubuntu:~$ pgrep apache gaurav@ubuntu:~$
With any output returned by the pgrep command, it is proved that the process apache has been killed.
conclusion
In this tutorial, we learned about the pkill command and how it is used directly to kill process names. We also understand that the pgrep command is used to get the process id of a process by any specific user. The pgrep command of allows us to reconfirm if the process is killed.