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

OK.

One Comment

Add a Comment

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