How to count the cpu usage of a process

Sometimes we want to collect the cpu usage of the process, and usually use a script to make simple statistics.

Under Linux, the cpu time slice of the process can be obtained through the stat provided by procfs.

Or simply use some commands to get directly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash

FULL_PATH=$1

$@ > /dev/null 2>&1 &

while true ; do
PID=$(pidof ${FULL_PATH})
if [ -z "$PID" ]; then
exit 0
fi
echo "$(date) :: $FULL_PATH[$PID] $(ps -C ${FULL_PATH} -o %cpu | tail -1)%"
sleep 0.5
done

How to count the cpu usage of a process
https://blog.justforlxz.com/2023/05/24/How-to-count-the-cpu-usage-of-a-process/
作者
小竹
发布于
2023年5月24日
许可协议