Fra*_*ter 18 command-line cpu-load logging memory-usage
Similar to question How to log CPU load?, I would like to log memory of a process.
The process I want to log, is killed on a remote server, and I want to find out the CPU load and the memory usage just before it was killed.
[update]
Both Stefano Palazzo's neat little python script and
Micha?'s one line output values that are smaller than in top for CPU and Mem. Do you have an idea why?
output top:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2312 schXX 20 0 1241m 328m 58m S 100 0.3 11:56.68 MATLAB
Run Code Online (Sandbox Code Playgroud)
output Stefano Palazzo's python script:
python memlogger.py 2312
%CPU %MEM
76.00 0.20
76.00 0.20
Run Code Online (Sandbox Code Playgroud)
Mic*_*jer 16
You can create one-liner in shell:
logpid() { while sleep 1; do ps -p $1 -o pcpu= -o pmem= ; done; }
Run Code Online (Sandbox Code Playgroud)
to log process with pid=123 just:
logpid 123
Run Code Online (Sandbox Code Playgroud)
or to see and write log to file:
logpid $$ | tee /tmp/pid.log
Run Code Online (Sandbox Code Playgroud)
If you want other data to be logged, modify -o {this} options. See man ps section "STANDARD FORMAT SPECIFIERS" for available parameters to use. If you want different time resolution, change sleep {this} in function logpid().
小智 6
如果您正是在top统计数据之后,您可以top在批处理模式下运行,指定您所追求的进程的 pid。以这个页面的例子(http://www.dedoimedo.com/computers/linux-cool-hacks.html)如果你输入
top -b -d 10 -n 3 >> top-file
Run Code Online (Sandbox Code Playgroud)
您将“在批处理模式 (-b) 下运行 top。按照延迟 (-d) 标志的指定,它将每 10 秒刷新一次,总共 3 次迭代 (-n)。输出将被发送到一个文件。” 包括-p您可以指定pid您所追求的过程。这当然不仅仅是纯粹的 cpu 和 ram 返回,但它将包含 thos 字段。例如,在我的情况下,我会在使用以下命令监视 pid 9189 时得到以下信息top -b -d 10 -n 3 -p 9189 >> ~/top-file:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
9189 pmj27 20 0 1617016 808112 27916 S 0.0 0.3 1:36.63 gedit
Run Code Online (Sandbox Code Playgroud)
这个简单的 python 脚本应该可以完成您想要的操作:
import time
import string
import sys
import commands
def get_cpumem(pid):
d = [i for i in commands.getoutput("ps aux").split("\n")
if i.split()[1] == str(pid)]
return (float(d[0].split()[2]), float(d[0].split()[3])) if d else None
if __name__ == '__main__':
if not len(sys.argv) == 2 or not all(i in string.digits for i in sys.argv[1]):
print("usage: %s PID" % sys.argv[0])
exit(2)
print("%CPU\t%MEM")
try:
while True:
x,y = get_cpumem(sys.argv[1])
if not x:
print("no such process")
exit(1)
print("%.2f\t%.2f" % (x,y))
time.sleep(0.5)
except KeyboardInterrupt:
print
exit(0)
Run Code Online (Sandbox Code Playgroud)
你首先需要找出你想要监控的程序的进程ID,然后你可以使用PID作为参数来运行脚本:
python log.py 3912
Run Code Online (Sandbox Code Playgroud)
它将每秒打印两次 cpu 使用率和 ram 使用率百分比:
%CPU %MEM
0.90 0.40
1.43 0.40
8.21 0.40
...
Run Code Online (Sandbox Code Playgroud)
然后,您可以将其输出重定向到文件,以便稍后将其导入到电子表格中 ( python log.py 9391 > firefox_log.txt),并将数据导入到选择Tab作为分隔符的电子表格中。
当您按 Ctrl+C 或进程被终止时,程序将退出。