How to find a specified container according to the process in shell
This article mainly explains "how to find a specified container according to the process in shell". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to find a specified container according to the process in shell.
When we use the top command on a docker server to locate a highly resource-intensive process, how to find out which container it belongs to.
1. Get the process pid first
2. Use the docker top command to find container pid
Docker top container life or container id
When there are too many docker containers, it takes a lot of work to check and compare one by one. Here is a script that you can find quickly.
#! / bin/bash# enter the pid you need to find into the variable $1 # import the pid of all containers into the variable aa= `docker ps-a | sed-n '1docker p' | awk' {print $1}'`# import the container id loop into the docker top command, and find the corresponding pidfor bin $adoc= `docker top $b | sed-n '1docker p' | awk' {print $2}'` # compare if with input $1 through the pid of each container [$c = = $1] Then# outputs container information corresponding to $1 docker ps-a | grep $cfidone
Add: shell finds the process and terminates
Create a kill.sh file as follows:
Port=9200# 1. Query the corresponding pid according to the port number. Both pid=$ (netstat-nlp | grep: $port | awk'{print $7}'| awk-F "/"{print $1}'); # pid=$ (ps-ef | grep your process or port | grep-v grep | awk'{print $2}') # II. Kill the corresponding process. If pid does not exist, if [- n" $pid "]; then kill-9$ pid; fi will not be executed.
This script is actually only 2 steps, first get the process id, and then kill the process.
(1) the method of obtaining process id
This can be obtained with the awk command
Ps-ef | grep your process | grep-v grep | awk'{print $2}'
Here, we will filter out the grep with-v, and then use the awk command to extract the second parameter, which is the process id.
(2) method of killing process
This will go directly to the kill-9 process id and ok.
Kill-9 your process id so far, I believe you have a deeper understanding of "how to find a specified container according to the process in shell". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!