#!/bin/bash ################################################################################ # A simple program to log memory usage of a command. # # Usage: memlog [] # # The logfile written is ./.log and has the form: # YYYYMMDD HH:MM:SS| ################################################################################ PROGNAME=$(basename $0) ###### # Write to the log file ###### log() { DT=$(date +"%Y%m%d %H:%M:%S:") echo "$DT|$1 $2" >>$LOG } ###### # Give user some help ###### usage() { echo "Usage: $PROGNAME []" echo "This will attempt to start the ' []' program, and will" echo "write a log file at ./.log." exit 10 } ###### # Get parameters. ###### if [ $# -lt 1 ]; then usage fi CMD=$* LOG="$1.log" ###### # Start command ###### $CMD & PID=$! ###### # Loop, getting $PID memory usage. ###### while true; do sleep 1 DATA=$(ps -o rss,vsize -p $PID | tail -1) RSS=$(echo $DATA | awk '{print $1}') VSIZE=$(echo $DATA | awk '{print $2}') # if child process finished, break out if [ "$RSS" == "RSS" ]; then break fi log $RSS $VSIZE done