监控某个进程的cpu开销
#!/bin/bash
PID=5586
FileName="res.txt"
echo -en "%CPU\t%MEM\t$PID" > $FileName
for (( i = 0; i < 30; i++ )) do
ttime=`date +"%m-%d %H:%M:%S"`
output=`top -n 1 -p $PID > tmp`
res=`gawk '{if (NR == 8) { printf "%d\t%d", $10, $11 } }' tmp`
echo -en "$res\t$ttime\n" >> $FileName
sleep 1
done
监控每秒cpu占用最高的Pid vi a.sh
#!/bin/bash
FileName="res.txt"
echo -en "%CPU\t%MEM\t%PID\t%TIME" > $FileName
for (( i = 0; i < 30; i++ )) do
ttime=`date +"%m-%d %H:%M:%S"`
output=`top -i -b -n 1 > tmp`
res=`gawk '{if (NR == 8 && $9>1) { printf "%d\t%d\t%d", $9, $10,$1 } }' tmp`
if [ -n "$res" ] ; then
echo -en "$res\t$ttime\n" >> $FileName
fi
sleep 1
done
然后用nohup sh a.sh &