Linux batch kill process (kill process according to keyword)

Source: Internet
Author: User
Tags fpm


A process is a symbol of the execution of a program in the computer, and the corresponding process terminates when the program is finished executing. But in reality many processes do not die at the end of the execution of the program, so they become zombies, and a small number of zombies do not affect the performance of the server, but no server can provide good performance in many processes.


Sometimes, because of some special circumstances, you need to kill all processes under Linux that match a certain condition, and you can't use Killall to kill all of the running processes that a process name contains (we may just have to kill one or the process of running a specified parameter command), and we need to use PS, grep, cut and kill operate together.

Look at the process first (take PHP for example):

PS Ef|grep php
Snapshot2

View the number of processes

PS Ef|grep php|wc-l

Kill the process

Ps-ef|grep php-fpm|grep-v grep|cut-c 9-15|xargs kill-9

Batch kills the process that contains the keyword "php-fpm".

"Ps-ef"--View all processes
"Grep php-fpm"--Lists all processes that contain the keyword "PHP-FPM"
grep-v grep-Removes processes that contain keyword "grep" in the listed process (because the grep process we generated in the previous step also contains keywords)
"Cut-c 9-15″--intercepts the 9th character of the input line to the 15th character, which happens to be the process number PID
The Xargs Kill-9″--xargs command is used to take the output (PID) of the previous command as an argument to the Kill-9″ command and execute the command. "Kill-9″ will be forced to kill the specified process.

In other similar cases, just modify the keyword section of the "grep local=no".

Another way to use awk

PS x|grep gas|grep-v grep |awk ' {print $} ' |xargs kill-9

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.