Skip to content

Instantly share code, notes, and snippets.

@WeiTang114
Last active March 21, 2017 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WeiTang114/68f5e98f6c60421041e3538b35df8bfa to your computer and use it in GitHub Desktop.
Save WeiTang114/68f5e98f6c60421041e3538b35df8bfa to your computer and use it in GitHub Desktop.
Find container ID and name by a process name (or part of the command).
#!/bin/bash
DOCKERUSER='ns1-root'
KEY="$1"
function usage() {
echo "Usage: $0 <keyword>"
}
if [[ $# -lt 1 ]]; then
usage && exit -1
fi
docker ps | tail -n +2 | \
awk '{print $1 " " $NF}' | \
xargs -I {} sh -c 'echo {}; p=`echo "{}"|cut -d " " -f 1`; docker top $p | grep '"$KEY" |\
egrep "$DOCKERUSER" -B 1
@WeiTang114
Copy link
Author

WeiTang114 commented Mar 21, 2017

Example:

$ ./dockerpgrep.sh vim


b4271d425577 aName1
ns1-root            67773               49222               0                   Mar15               pts/1               00:00:00            vim model.py
--
8abc44f9039b aName2
ns1-root            73589               63578               0                   Mar14               pts/0               00:00:06            vim input.py
--
adc4d7f328aa aName3
ns1-root            7740                76978               0                   Mar16               pts/2               00:00:10            vim SBIR.php
--
42bd03cff8db aName4
ns1-root            10312               17858               0                   Mar14               pts/0               00:00:34            vim model_batchlet.py
507f40593f51 aName5
ns1-root            5660                39211               0                   Mar13               pts/30              00:00:03            vim model.py
ns1-root            10078               39211               0                   Mar13               pts/30              00:00:03            vim model.py
ns1-root            64663               3548                0                   Mar14               pts/0               00:00:15            vim train.py
--
54d1e78775f1 aName6
ns1-root            9028                35000               0                   Feb19               pts/10              00:00:00            vim Makefile.config

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment