Search code examples
linuxawkcontainerskubectlps

Grep the PIDs of simulator and kill the same using kubectl


I need help with the command where I am trying to grep the PIDs of ecm simulator and kill the same using kubectl :

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

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

Then further I need to kill the PIDs:

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

When I am executing the same within the service pod it's working but it's giving issues when I am doing via kubectl from outside.

Could anyone please let me know what I am doing wrong here?

NOTE: There are 2 PIDs which needs to be killed from the below output:

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

EDIT:

Output of the command as asked by @Cyrus below:

enter image description here


Solution

  • Posting this this as Community Wiki answer for better visibility. Solution has been provided in comments by @Cyrus.

    In Short, OP wanted to Kill/interrupt some process using their PID's. OP wanted to do it from cluster level on specific pod/container which included ecm simulator.

    To do it, commands below were used:

    • 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

    In MANUAL you can find some information about ps flags:

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

    however each flag will still give another output, like below:

    -e
     PID TTY  TIME CMD
    
    -ef
    UID  PID  PPID  C STIME TTY  TIME CMD
    

    Cyrus advised to use following command:

    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.

    Also explain in comment:

    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.