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.

#!/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