Skip to content

Instantly share code, notes, and snippets.

@maartenbosteels
Created November 28, 2023 08:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maartenbosteels/51c0cd6e7de90b49ddd25e8668a1d80b to your computer and use it in GitHub Desktop.
Save maartenbosteels/51c0cd6e7de90b49ddd25e8668a1d80b to your computer and use it in GitHub Desktop.
monitor memory and cpu usage of a single process
PID=$1
echo "Monitoring CPU usage of process with PID ${PID}"
ps -ef | grep $PID
OUTPUT_FOLDER=~/.process_stats
mkdir -p ${OUTPUT_FOLDER}
MEM_STATS=${OUTPUT_FOLDER}/mem_${PID}.txt
CPU_STATS=${OUTPUT_FOLDER}/cpu_${PID}.txt
echo "" > $MEM_STATS
echo "" > $CPU_STATS
echo "CPU stats will be stored in ${CPU_STATS}"
echo "Memory stats will be stored in ${MEM_STATS}"
keep_running="yes"
trap 'keep_running="no"' 2
while [ ${keep_running} == "yes" ]; do
ps -p ${PID} -o %cpu= >> ${CPU_STATS};
# percentage memory:
# ps -p ${PID} -o %mem= >> ${MEM_STATS};
ps -p ${PID} -o rss= >> ${MEM_STATS};
sleep 1
done
echo "Done monitoring CPU usage of process with PID ${PID}"
MEM_PNG=${OUTPUT_FOLDER}/procplot.mem.${PID}.png
CPU_PNG=${OUTPUT_FOLDER}/procplot.cpu.${PID}.png
# Plot
gnuplot<<<"set term png; set output \"${MEM_PNG}\" ; plot \"${MEM_STATS}\" with boxes"
gnuplot<<<"set term png; set output \"${CPU_PNG}\" ; plot \"${CPU_STATS}\" with boxes"
open ${CPU_PNG} ${MEM_PNG}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment