top

# use top to monitor process average cpu consumption in batch mode
# PID is set in beginning to avoid multiple changes
# Example below should perform 50 checks with delay 0.1 seconds and prints out average using awk.

# PID=1234; top -b -d 0.1 -n 50 -p ${PID}|sed -e 's/^[ \t]*//'| grep "^${PID}" |awk 'BEGIN{CPU=0;COUNT=0;}{CPU=CPU+$9;COUNT++}END{print int(CPU/COUNT);}'
7

# same thing, without sed and grep

# PID=2798; top -b -d 0.1 -n 50 -p ${PID} |awk -v PID=${PID} 'BEGIN{CPU=0;COUNT=0;}{if($1 == PID){CPU=CPU+$9;COUNT++};}END{print int(CPU/COUNT);}'
7

If you found this useful, say thanks, click on some banners or donate, I can always use some beer money.