Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of killing the process by linux

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "what are the methods of linux killing process", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "what are the methods of linux killing process" can help you solve your doubts? let's follow the editor's way of thinking to learn new knowledge.

General rules:

First, use ps to view the process as follows:

The copy code is as follows:

$ps-ef

$ps-ef

……

Smx 1822 1 0 11:38? 00:00:49 gnome-terminal

Smx 1823 1822 0 11:38? 00:00:00 gnome-pty-helper

Smx 1824 1822 0 11:38 pts/0 00:00:02 bash

Smx 1827 1 4 11:38? 00:26:28 / usr/lib/firefox-3.6.18/firefox-bin

Smx 1857 1822 0 11:38 pts/1 00:00:00 bash

Smx 1880 1619 0 11:38? 00:00:00 update-notifier

……

Smx 11946 1824 0 21:41 pts/0 00:00:00 ps-ef

Or:

The copy code is as follows:

$ps-aux

……

Smx 1822 0.1 0.8 58484 18152? Sl 11:38 0:49 gnome-terminal

Smx 1823 0.0 0.0 1988 712? S 11:38 0:00 gnome-pty-helper

Smx 1824 0.0 0.1 6820 3776 pts/0 ss 11:38 0:02 bash

Smx 1827 4.3 5.8 398196 119568? Sl 11:38 26:13 / usr/lib/firefox-3.6.18/firefox-bin

Smx 1857 0.0 0.1 6688 3644 pts/1 ss 11:38 0:00 bash

Smx 1880 0.0 0.6 41536 12620? S 11:38 0:00 update-notifier

……

Smx 11953 0.0 2716 1064 pts/0 r + 21:42 0:00 ps-aux

At this point, if I want to kill Firefox, the process will be typed on the terminal:

The copy code is as follows:

$kill-s 9 1827

Among them,-s 9 establishes that the signal transmitted to the process is 9, that is, to force and terminate the process as soon as possible. Each termination signal and its function are shown in the appendix.

1827 is the pid of Firefox found by ps above.

Simple, but there is a problem, it doesn't matter if there are fewer processes. If there are more processes, you will feel more painful. No matter it is ps-ef or ps-aux, you have to find the process you want to kill in a long list of process information every time.

Advanced chapters:

Improvement 1:

Pipe the query results of ps to grep to find the process that contains a specific string. The pipe character "|" is used to separate two commands, and the output of the command on the left side of the pipe character is used as the input of the command on the right side of the pipe character.

The copy code is as follows:

$ps-ef | grep firefox

Smx 1827 1 4 11:38? 00:27:33 / usr/lib/firefox-3.6.18/firefox-bin

Smx 12029 1824 0 21:54 pts/0 00:00:00 grep-color=auto firefox

It will be refreshing this time. And then there is.

The copy code is as follows:

$kill-s 9 1827

Improved 2mi-using pgrep:

What's the first thing that comes to mind when you see pgrep? That's right, grep! The p of pgrep indicates that this command is a grep dedicated to process queries.

The copy code is as follows:

$pgrep firefox

1827

What do you see? Yes, the pid of Firefox, and then I'm going to type again:

$kill-s 9 1827

Improved 3mure-using pidof:

What do you think when you see pidof? That's right, pid of xx, which literally translates to xx's pid.

The copy code is as follows:

$pidof firefox-bin

1827

A slight disadvantage compared with pgrep is that pidof must give the full name of the process. And then there's the clich é:

The copy code is as follows:

$kill-s 9 1827

Whether you use ps and then slowly find the process pid or use grep to find the process containing the corresponding string, or use pgrep to directly find the process pid that contains the corresponding string, and then manually enter it into kill to kill, it is a bit troublesome. Is there a more convenient way? Yes!

Improvement 4:

The copy code is as follows:

$ps-ef | grep firefox | grep-v grep | cut-c 9-15 | xargs kill-s 9

Description:

The output of "grep firefox" is all processes with the keyword "firefox".

"grep-v grep" is a process that removes the keyword "grep" from the listed processes.

"cut-c 9-15" intercepts the 9th to 15th characters of the input line, and this happens to be the process number pid.

The xargs command in "xargs kill-s 9" is used to take the output of the previous command (pid) as an argument to the "kill-s 9" command and execute the command. "kill-s 9" forcibly kills the specified process.

Don't you want to complain about something? Yeah, it's too long.

Improvement 5:

Why do you have to play such a long string when you know the two commands of pgrep and pidof?

The copy code is as follows:

$pgrep firefox | xargs kill-s 9

Improvement 6:

The copy code is as follows:

$ps-ef | grep firefox | awk'{print $2}'| xargs kill-9

Kill: no such process

There is a more depressing place, the process has been correctly found and terminated, but the completion of the execution indicates that the process cannot be found.

The function of awk'{print $2}'is to print the contents of the second column. From the regular article, you can see that the second column of the ps output happens to be pid. Pass the corresponding pid of the process to kill through xargs as a parameter to kill the corresponding process.

Improvement 7:

Do I have to call xargs every time to pass pid to kill? The answer is no:

The copy code is as follows:

$kill-s 9 `ps-aux | grep firefox | awk'{print $2}'

Improvement 8:

Yes, the command is still a little long. Replace it with pgrep.

The copy code is as follows:

$kill-s 9 `pgrep firefox`

Improve 9--pkill:

What do you think of when you see pkill? Yes, pgrep and kill! Pkill=pgrep+kill .

The copy code is as follows:

$pkill-9 firefox

Note: "- 9" that is, the signal sent is 9. The difference between kill and pkill at this point is that pkill does not need "s", and the termination signal level is directly followed by "-". I always thought it was "- s 9", but each run failed to terminate the process.

Improve 10--killall:

Killall and pkill are similar, but if the process name given is incomplete, killall will report an error. Pkill or pgrep can terminate a process as long as part of the process name is given.

The copy code is as follows:

$killall-9 firefox

After reading this, the article "what are the methods of linux killing process" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report