A script was submitted in the Shell. The process number is no longer known, but the process needs to be killed. How to operate?

ssh $i "ps -ef | grep XXX | grep -v grep |awk '{print $2}' | xargs kill"

In fact, it searches for the PID of a certain type of process according to the XXX keyword and then kills it, that is, batch kills the processes that contain a certain keyword

For example: search for process PID that contains spark keyword

[root@bigdata-1 phm]# ps -ef | grep spark | grep -v grep |awk '{print $2}' 
12978
17073

The essence of this problem is to search for the task name keyword in the shell script, print out the process id and then kill it.

Explanation of key terms in the script:

  • The pipe character "|" is used to separate two commands, and the output of the command on the left side of the pipe character will be used as the input of the command on the right side of the pipe character.
  •  
  • "ps -ef" is the command to view all processes in linux . The process retrieved at this time will be used as the input of the next command "grep spark".
  •  
  • The output of "grep spark" is all processes that contain the keyword "spark".
  •  
  • "grep -v grep" removes the processes containing the keyword "grep" from the listed processes.
  •  
  • "awk'{print $2}" is the second column of the line to be filtered, and the second column is exactly the process number PID.
  •  
  • The xargs  command in "xargs kill -9"  is used to take the output (PID) of the previous command as the parameter of the "kill -9" command and execute the command. "kill -9" will forcefully kill the specified process.

Note: This command is to kill a type of process (multiple) with specified parameters (keywords) or a process that runs a command with specified parameters.

In addition, the command can also be written like this:

ps -ef | grep spark | grep -v grep | cut -c 9-15 | xargs kill -9

Replace awk'{print $2}' with cut -c 9-15, but it is more intuitive to use awk

[root @ bigdata-1 phm] # ps -ef | grep spark | grep -v grep | cut -c 9-15
 12978
 17073

 

Guess you like

Origin blog.csdn.net/godlovedaniel/article/details/109139575