A shell script for monitoring memory usage
I guess many people wrote this kind of scripts already. But sometimes searching is more complicated than just writing a new one. 😅 If you know a keyword, please tell me.
This script should work with other commands. I use target/release/main because I want to test my program built from Rust's Cargo. I suppose other commands can replace target/release/main.
#!/bin/sh
CMD=target/release/main
$CMD > log 2>&1 &
export PID=$!
echo "Process $PID is running ..."
trap "echo kill $PID; kill $PID; exit" INT QUIT
rm -f mem.tsv
while true
do
tm=`date '+%Y-%m-%dT%H:%M:%S'`
rss=`ps -u | awk -v PID=$PID '{ if ($2 == PID) { print $6; } }'`
echo "$tm\t$rss" >> mem.tsv
sleep 10
done
It logs memory usage in mem.tsv, so I should be able to plot a graph by LibreOffice Calc easily.