Awk tutorial: find and kill process use awk

Usually we often need to kill the service process when running the server, so let’s see how to use the linux awk command to kill the process.

As shown above, the system is running the top command, how to use awk kill it?

Of course, when we need to kill a single process, you can run the kill command after viewing the process ID through top, but if you need to batch kill, using the shell command will be much more efficient.

Step 1. View the process ID pid of the top command.

➜  ~ ps -ef | grep top
#ps -ef | awk '/top/'

It is best to use grep -v to exclude the grep command itself.

➜  ~ ps -ef | grep top | grep -v grep

Step 2. Use the awk command to get the process ID

➜  ~ ps -ef | grep top | grep -v grep | awk '{print $2}'

Command result 7630 is the top command pid.

Step 3. Use xargs kill to kill top process ID.

➜  ~ ps -ef | grep top | grep -v grep | awk '{print $2}' | xargs kill -9


Related post

awk command tutorial in linux/unix with examples and use cases
Awk tutorial: awk merge files base on the keyword
AWK tutorial: awk custom function and awk example
Awk tutorial: awk -F and awk BEGIN{ FS … }
Awk tutorial: awk regex example
Awk tutorial: awk loop example: awk for and while
awk if else, if else if else and awk if else nested example
Awk tutorial: awk NR FNR difference
Awk tutorial: awk sort uniq
Awk tutorial: split on character awk or cut
Awk tutorial: awk newline character
Awk tutorial: three ways of awk execution
How to print tree directory structure in linux/unix
Linux shell batch move files, delete spaces, rename files

Add a Comment

Your email address will not be published. Required fields are marked *