Retrieve CPU usage and memory usage of a single process on Linux?

前端 未结 21 1976
抹茶落季
抹茶落季 2020-11-27 09:00

I want to get the CPU and memory usage of a single process on Linux - I know the PID. Hopefully, I can get it every second and write it to a CSV using the \'watch\' command

21条回答
  •  萌比男神i
    2020-11-27 09:47

    Based on @caf's answer, this working nicely for me.

    Calculate average for given PID:

    measure.sh

    times=100
    total=0
    for i in $(seq 1 $times)
    do
       OUTPUT=$(top -b -n 1 -d 0.1 -p $1 | tail -1 | awk '{print $9}')
       echo -n "$i time: ${OUTPUT}"\\r
       total=`echo "$total + $OUTPUT" | bc -l`
    done
    #echo "Average: $total / $times" | bc
    
    average=`echo "scale=2; $total / $times" | bc`
    echo "Average: $average"
    

    Usage:

    # send PID as argument
    sh measure.sh 3282
    

提交回复
热议问题