Skip to content

Instantly share code, notes, and snippets.

@Kerollmops
Last active July 24, 2021 12:55
Show Gist options
  • Save Kerollmops/f2ac8c5a7c214dd547c301fb02a4fe18 to your computer and use it in GitHub Desktop.
Save Kerollmops/f2ac8c5a7c214dd547c301fb02a4fe18 to your computer and use it in GitHub Desktop.
Record memory usage of a process
#!/usr/bin/env bash
# usage: memlog.sh $(pidof PROCESS_NAME) [ PATH_FOLDER ]
# on osx pidof can be replaced by pgrep
# usage: memlog.sh $(pgrep PROCESS_NAME) [ PATH_FOLDER ]
set -e
PID=$1
FOLDER=$2
LOG=./$PID.csv
echo recording memory usage of $PID
echo -n 'cpu,rss' > $LOG
if [[ -n "$FOLDER" ]]; then
echo recording $FOLDER disk usage
echo -n ',disk' >> $LOG
fi
echo >> $LOG
echo log file: $LOG
while kill -0 $PID; do
if [[ -z "$FOLDER" ]]; then
ps -p $PID -o\%cpu,rss | awk 'NR>1 { gsub(",",".",$1); printf $1","$2}' >> $LOG
else
ps -p $PID -o\%cpu,rss | awk 'NR>1 { gsub(",",".",$1); printf $1","$2}' >> $LOG
printf ',' >> $LOG
du -sm $FOLDER | cut -f -1 >> $LOG
fi
sleep 0.2
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment