Last active
February 14, 2023 14:03
-
-
Save holyjak/931a3441982c833f5f8fcdcf54d05c91 to your computer and use it in GitHub Desktop.
Script to monitor the usage of CPU, memory by a process via `top`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
## | |
## BEWARE: Check with your impl. of top what exactly it returns, it migth differ from mine | |
## | |
# Usage: ./monitor-usage.sh <PID of the process> | |
# Output: top.dat with lines such as `1539689171 305m 2.0`, i.e. unix time - memory with m suffix - CPU load in % | |
# To plot the output, see https://gist.github.com/holyjak/1b58dedae3207b4a56c9abcde5f3fdb5 | |
export PID=$1 | |
rm top.dat | |
while true; do top -p $PID -bn 1 -em | egrep '^ *[0-9]+' | awk -v now=$(date +%s.%N) '{print now,$6,$9}' >> top.dat; done | |
# top: -p <pid> target process, -b batch mode, -n 1 run once; -em display mem in MB | |
# egrep extracts the line starting with the pid, with the metrics | |
# awk prepends a date and extracts columns 6 (RES = residential memory, ie RAM) and 9, which should be the memory and cpu load |
@sikmir Thank you! I am not sure what version of Top I have used at that time. I've adjusted it for a linux top I have available now.
One more fix:
--- monitor-usage.sh.orig 2023-02-14 16:57:30.000000000 +0300
+++ monitor-usage.sh 2023-02-14 16:56:52.000000000 +0300
@@ -4,7 +4,7 @@
##
# Usage: ./monitor-usage.sh <PID of the process>
# Output: top.dat with lines such as `1539689171 305m 2.0`, i.e. unix time - memory with m suffix - CPU load in %
-# To plot the output, see https://gist.github.com/jakubholynet/931a3441982c833f5f8fcdcf54d05c91
+# To plot the output, see https://gist.github.com/holyjak/1b58dedae3207b4a56c9abcde5f3fdb5
export PID=$1
rm top.dat
while true; do top -p $PID -bn 1 -em | egrep '^ *[0-9]+' | awk -v now=$(date +%s.%N) '{print now,$6,$9}' >> top.dat; done
thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
top: unknown option 'M'