Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Created August 28, 2024 14:04
Show Gist options
  • Save kristopherjohnson/c11e9f535d9512efdbd96af056119224 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/c11e9f535d9512efdbd96af056119224 to your computer and use it in GitHub Desktop.
Once per minute, collect process CPU and memory-usage in CSV format
#!/bin/bash
# Given a process ID, print time,%cpu,%mem,vsz,rss in CSV format once per minute.
# Check if PID is provided
if [ -z "$1" ]; then
echo "Usage: $0 <PID>"
exit 1
fi
PID=$1
echo 'timestamp,%cpu,%mem,vsz,rss'
while true; do
# Get the current time in ISO 8601 format
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
# Run the ps command and format the output
ps -p $PID -ww -O %cpu,%mem,vsz,rss | sed '1d' | awk -v timestamp="$TIMESTAMP" '{print timestamp","$2","$3","$4","$5}'
# Sleep for 60 seconds
sleep 60
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment