How to use part of the name given to kill all processes?

I want to kill all processes by:

ps aux | grep my_pattern

How to do it?

This does not work:

pkill my_pattern

#1st Floor

Kill all the strings "myProcessName" matching process:

ps -ef | grep 'myProcessName' | grep -v grep | awk '{print $2}' | xargs -r kill -9

Source: HTTP : //www.commandlinefu.com/commands/view/1138/ps-ef-grep-process-grep-v-grep-awk-print-2-xargs-kill-9

What this code is doing?

ps -efGenerate a list of process ID on the user's computer is visible. Grep filtered off conduit line containing the string. grep -v grepSay does not match the process execution grepping itself. Awk print pipeline that will default delimiters blank row splitting and filtering to the second column, this is our process ID. Xargs pipeline started a new process to send all these pid kill -9, so all over.

The above code is bad, dangerous, ugly and unpleasant, for several reasons.

  1. If the code is being forced to terminate operations or perform any database with a low probability of competitive conditions for secure transactions, so a fraction of one percent of the time, atomicity of the transaction will be destroyed, resulting in undefined behavior. No need to kill -9 prisoners. If you are sensitive to this code, please try to xargs killpartially replace the normally closed transmission request flag, and only when the request is rejected, last-resort tokill -9

  2. You have a non-zero possibility of unexpectedly terminate or lead to undefined operating system behavior in an unrelated process, leading to instability in the whole system, because ps -efa list of every possible process that may exist, and you can not determine whether a strange third-party libraries to share your process name, or the time between the read and execute kill -9, processid has been changed to another name, now you accidentally terminate some of you are not using a random process.

However, if you understand the risks in a very unique name and to control it, and can accept small amount of the transaction is lost or corrupted data occasionally, then 99.9% of the time on it. If you have questions, please restart your computer, ensuring that no conflict process. It is because of such a code would allow technology support script become a "five meme."


#2nd Floor

You can use the following command to list the process

ps aux | grep -c myProcessName 

If you need to check the number of the process, run

ps aux | grep -c myProcessName |grep -v grep 

After that, you can use the following command to terminate the process

kill -9 $(ps aux | grep -e myProcessName | awk '{ print $2 }') 

#3rd floor

It does not sound right?

 pkill `pidof myprocess`

Example:

# kill all java processes
pkill `pidof java`

#4th floor

If you think pkill -f PATTERNa little too dangerous, I'll write ezkill a bash script, you are prompted to select which process to kill with the PATTERN matching process.


#5th Floor

You can use the following command:

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

Or

ps -aux | grep -i myprocess | awk {'print $2'} | xargs kill -9

This is useful for me.


#6th floor

You can use the following command

kill -9 $(ps aux | grep 'process' | grep -v 'grep' | awk '{print $2}')

#7th floor

You can also use killall -r my_pattern. -rProcess Name mode interpreted as extended regular expressions.

killall -r my_pattern

Building # 8

If you do not want to bother find the process ID, use regexp kill process by name. For example, killing chrome can solve the problem.

killall -r --regexp chrome


House # 9

The best pgrep -fwith killor pkill -fused together are the best and safest, grep psthe output may be wrong.

And use ps | grep ps | grep, you will need to add | grep -vfilter out rows grep | grep -vor usage patterns skill, pgrepnot by design choice.

And, if your model appears in psthe UID/ USER, SDATE/ STARTor any other column, you will not need to get in the output of the process and kill them, pgrep+ pkillwill not be affected by this defect.

I also found killall -r / -regexp expression is not compatible with my timing.

pkill -f "^python3 path/to/my_script$"

man pkill


#10th floor

We found no support for pkillthe best way to do this server

kill -9 $(ps ax | grep My_pattern| fgrep -v grep | awk '{ print $1 }')

You do not have to cycle.


House # 11

Used pkill -f, it matches any part of the command line pattern

pkill -f my_pattern

House # 12

If you need greater flexibility in the selection process, please use

for KILLPID in `ps ax | grep 'my_pattern' | awk ' { print $1;}'`; do 
  kill -9 $KILLPID;
done

You can use grep -e and so on.

Original articles published 0 · won praise 0 · Views 2210

Guess you like

Origin blog.csdn.net/p15097962069/article/details/103925579