Skip to content

Instantly share code, notes, and snippets.

@3inar
Created October 27, 2014 15:57
Show Gist options
  • Save 3inar/f362f6469036c023df4e to your computer and use it in GitHub Desktop.
Save 3inar/f362f6469036c023df4e to your computer and use it in GitHub Desktop.
A thing for tracking cpu+mem usage for a single process. I don't remember where it's from
#!/bin/bash
# --- Version history ---
# 0.4: added variable to store file path, and $2 for base file name
# added variable to store desired reporting interval
# 0.3: added $1 to send in process ID at run time.
# 0.2: switched to $SECONDS for the loop. works.
# 0.1: didn't work well at all.
# --- Version history ---
# Usage: cputrack [PID] [filename]
# replace [PID] with process ID #
# replace [filename] with base file name to use (no extension)
filepath=/tmp/eho033 # modify as desired
interval=60 # reports per minute
timelimit=50400
mydate=`date "+%H:%M:%S"` # the timestamp
freq=$((60/$interval)) # for sleep function
while [ "$SECONDS" -le "$timelimit" ] ; do
ps -p$1 -opid -opcpu -ocomm -o%cpu -o%mem -c | grep $1 | sed "s/^/$mydate /" >> $filepath/$2.txt
sleep 3
mydate=`date "+%H:%M:%S"`
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment