Skip to content

Instantly share code, notes, and snippets.

@yougg
Created August 17, 2018 09:09
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 yougg/b06f50559b930344822646259bdf729c to your computer and use it in GitHub Desktop.
Save yougg/b06f50559b930344822646259bdf729c to your computer and use it in GitHub Desktop.
monitor process state by pid
#!/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