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

How to forcibly kill a process in Linux

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces how to force the killing process in Linux, the content is very detailed, interested friends can refer to, hope to be helpful to you.

General rules:

First, use ps to view the process as follows:

$ps-ef

…… Smx 1822 1 0 11:38? 00:00:49 gnome-terminalsmx 1823 1822 11:38? 00:00:00 gnome-pty-helpersmx 1824 1822 0 11:38 pts/0 00:00:02 bashsmx 1827 1 4 11:38? 00:26:28 / usr/lib/firefox-3.6.18/firefox-binsmx 1857 1822 0 11:38 pts/1 00:00:00 bashsmx 1880 1619 0 11:38? 00:00:00 update-notifier. Smx 11946 1824 0 21:41 pts/0 00:00:00 ps-ef

Or:

$ps-aux

…… Smx 1822 0.1 0.8 58484 18152? Sl 11:38 0:49 gnome-terminalsmx 1823 0.0 0.0 1988 712? S 11:38 0:00 gnome-pty-helpersmx 1824 0.0 0.1 6820 3776 pts/0 Ss 11:38 0:02 bashsmx 1827 4.3 5.8 398196 119568? Sl 11:38 26:13 / usr/lib/firefox-3.6.18/firefox-binsmx 1857 0.0 0.1 6688 3644 pts/1 Ss 11:38 0:00 bashsmx 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:

$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.

$ps-ef | grep firefox

Smx 1827 1 4 11:38? 00:27:33 / usr/lib/firefox-3.6.18/firefox-binsmx 12029 1824 0 21:54 pts/0 00:00:00 grep-color=auto firefox

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

$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.

$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.

$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 é:

$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

Improvement 4:

$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.

Improvement 5:

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

$pgrep firefox | xargs kill-s 9

Improvement 6:

$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:

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

Improvement 8:

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

$kill-s 9 `pgrep firefox`

Improve 9--pkill:

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

$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.

$killall-9 firefox

Appendix: various signals and their uses

SignalDescriptionSignal number on Linux x86 [1] SIGABRTProcess aborted6SIGALRMSignal raised by alarm14SIGBUSBus error: "access to undefined portion of memory object" 7SIGCHLDChild process terminated Stopped (or continued*) 17SIGCONTContinue if stopped18SIGFPEFloating point exception: "erroneous arithmetic operation" 8SIGHUPHangup1SIGILLIllegal instruction4SIGINTInterrupt2SIGKILLKill (terminate immediately) 9SIGPIPEWrite to pipe with no one reading13SIGQUITQuit and dump core3SIGSEGVSegmentation violation11SIGSTOPStop executing temporarily19SIGTERMTermination (request to terminate) 15SIGTSTPTerminal stop signal20SIGTTINBackground process attempting to read from tty ("in") 21SIGTTOUBackground process attempting to write to tty ("out") 22SIGUSR1User-defined 110SIGUSR2User-defined 212SIGPOLLPollable event29SIGPROFProfiling timer expired27SIGSYSBad syscall31SIGTRAPTrace/breakpoint trap5SIGURGUrgent data available on socket23SIGVTALRMSignal raised by timer counting virtual time: "virtual timer expired" 26SIGXCPUCPU time limit exceeded24SIGXFSZFile size limit exceeded25 what is the Linux system Linux is a free use And free spread UNIX-like operating system Is a POSIX-based multi-user, multi-tasking, multi-threaded and multi-CPU operating system, using Linux to run major Unix tools, applications and network protocols.

On how to force the killing process in Linux to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Servers

Wechat

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

12
Report