Grep 模拟器的 PID 并使用 kubectl 杀死它

Grep the PIDs of simulator and kill the same using kubectl

我在尝试 grep ecm 模拟器的 PID 并使用 kubectl 杀死它时需要命令方面的帮助:

kubectl exec eric-service-0 -n cicd --kubeconfig /root/admin.conf -- bash -c "ps -ef | grep ecm | grep node | awk '{print }' "

Output of the above command:

root      9857     0  0 07:11 ?        00:00:00 bash -c /tmp/simulator/node-v8.11.3-linux-x64/bin/node /tmp/simulator/ecm_mod.js> /tmp/simulatorEcmResponse.txt
root      9863  9857  0 07:11 ?        00:00:00 /tmp/simulator/node-v8.11.3-linux-x64/bin/node /tmp/simulator/ecm_mod.js

Expected output is:

9857 
9863

然后我需要杀死 PID:

kubectl exec eric-service-0 -n cicd --kubeconfig /root/admin.conf -- bash -c "ps -ef | grep ecm | grep node | awk '{print }' | xargs kill -9"

当我在服务 pod 中执行相同的操作时,它可以正常工作,但是当我从外部通过 kubectl 执行时,它会出现问题。

谁能告诉我我做错了什么?

注意:有 2 个 PID 需要从以下输出中终止:

eric-service-0:/ # ps -ef | grep ecm | grep node
root      9857     0  0 07:11 ?        00:00:00 bash -c /tmp/simulator/node-v8.11.3-linux-x64/bin/node /tmp/simulator/ecm_mod.js> /tmp/simulatorEcmResponse.txt
root      9863  9857  0 07:11 ?        00:00:00 /tmp/simulator/node-v8.11.3-linux-x64/bin/node /tmp/simulator/ecm_mod.js

编辑:

下面@Cyrus 要求的命令输出:

将其发布为社区 Wiki 答案以获得更好的可见性。解决方案已在@Cyrus 的评论中提供。

简而言之,OP 想 Kill/interrupt 使用他们的 PID's 一些过程。 OP 想在特定 pod/container 上从集群级别进行,其中包括 ecm simulator.

为此,使用了以下命令:

  • exec - execute a command in a container
  • -- bash - run bash inside container
  • ps -ef - list all process on the system
  • grep - serch specific pattern
  • awk - pattern scanning and processing language.
  • xargs - build and execute command lines from standard input
  • kill - send a signal to a process

MANUAL 中您可以找到一些关于 ps 标志的信息:

To see every process on the system using standard syntax:
          ps -e
          ps -ef
          ps -eF
          ps -ely

但是每个标志仍会给出另一个输出,如下所示:

-e
 PID TTY  TIME CMD

-ef
UID  PID  PPID  C STIME TTY  TIME CMD

Cyrus 建议使用以下命令:

kubectl exec eric-service-0 -n cicd --kubeconfig /root/admin.conf -- bash -c "pgrep -f 'node.*ecm'"

bash -c - If the -c option is present, then commands are read from the first non-option argument command_string.

另在评论中说明:

pgrep looks through the currently running processes and lists the process IDs which match the selection criteria to stdout. From man pgrep. node.*ecm is a regex.