The awk and xargs commands in linux have always been a headache for me. Here first record an example of the combination of awk and xargs.
Assume the following processes are as follows.
root@master:/home/jxl/ascend_new/output/test_unit# ps -ef | grep start.sh
root 234 1 0 01:37 pts/1 00:00:00 /bin/bash start.sh
root 716 1 0 01:37 pts/1 00:00:00 /bin/bash start.sh
root 1225 1 0 01:37 pts/1 00:00:00 /bin/bash start.sh
root 1455 1 0 01:37 pts/1 00:00:00 /bin/bash start.sh
root 1701 1 0 01:37 pts/1 00:00:00 /bin/bash start.sh
root 1791 1 0 01:37 pts/1 00:00:00 /bin/bash start.sh
root 4238 17 0 01:43 pts/1 00:00:00 grep --color=auto start.sh
The requirement is that we delete the processes containing start.sh in batches, you can use the following command
ps -ef | grep start.sh | grep -v grep | awk '{print $2}' | xargs kill
The specific steps of the above command are to filter through start.sh, then filter out items containing grep, awk finds the parameter of the second process number, and then pass in the kill command through xargs to delete.