Created
August 17, 2018 09:09
-
-
Save yougg/b06f50559b930344822646259bdf729c to your computer and use it in GitHub Desktop.
monitor process state by pid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Thread state | |
# pidstat -dhut -p <PID> | |
# top -bcH -n 1 -p <PID> | |
# pstree -ps <PID> | |
pname=${1} | |
if [ -z "${pname}" ] || [[ "${pname}" =~ [-](h|H|[-]?help) ]]; then | |
echo -e "Error: need input a process name or pid\n" | |
echo "Usage:" | |
echo -e "\tpidstat.sh <process name|pid> [interval [count]]" | |
echo "Example:" | |
echo -e "\tpidstat.sh apimgr 1 5" | |
echo -e "\tpidstat.sh statistics 10" | |
echo -e "\tpidstat.sh \`pidof apimgr\` 1 10 > apimgr.stats" | |
exit 1 | |
fi | |
interval=${2} | |
if [ -z "${interval}" ] || [[ ! "${interval}" =~ [0-9]+ ]] || [ ${interval} -le 0 ]; then | |
interval=1 | |
fi | |
count=$3 | |
if [ -n "${count}" ]; then | |
if [[ ! "${count}" =~ [0-9]+ ]] || [ ${count} -le 0 ]; then | |
count=1 | |
fi | |
else | |
count=1 | |
fi | |
pidstat -dhIrsu -p SELF | grep '^#' | |
if [[ "${pname}" =~ [0-9]+ ]]; then | |
pids="-p ${pname}" | |
else | |
pids="-C ${pname}" | |
fi | |
x=0 | |
while [ ${x} -lt ${count} ]; do | |
pidstat -dhIrsu ${pids} 1 1 | grep -Ev '^Linux|^#|^$' | |
sleep ${interval} | |
x=$((x+1)) | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment