testFree.sh 脚本内容如下:
#!/bin/sh
#上一次记录时的剩余内存
preFree=4096000
#有多少秒未记录了
timeEsc=0
while true
do
stmp=`free -k | grep "Mem"`
OSMem_free=`echo $stmp | awk '{print $4}'`
let nDiff="$preFree-$OSMem_free"
let timeEsc="$timeEsc+1"
if [ $nDiff -ge 2048 ] || [ $nDiff -lt -2048 ] || [ $timeEsc -ge 6000 ] ; then
echo .
fi
done
执行脚本
./testFree.sh >> /dev/null
在另一个终端不停执行
# ps | grep testF | grep -v grep
24259 root 1752 S {testFree.sh} /bin/sh ./testFree.sh
# ps | grep testF | grep -v grep
24259 root 1752 S {testFree.sh} /bin/sh ./testFree.sh
29774 root 1752 S {testFree.sh} /bin/sh ./testFree.sh
# ps | grep testF | grep -v grep
24259 root 1752 S {testFree.sh} /bin/sh ./testFree.sh
# ps | grep testF | grep -v grep
24259 root 1752 S {testFree.sh} /bin/sh ./testFree.sh
# ps | grep testF | grep -v grep
1570 root 1752 S {testFree.sh} /bin/sh ./testFree.sh
24259 root 1752 S {testFree.sh} /bin/sh ./testFree.sh
# ps | grep testF | grep -v grep
3317 root 1752 S {testFree.sh} /bin/sh ./testFree.sh
24259 root 1752 S {testFree.sh} /bin/sh ./testFree.sh
# ps | grep testF | grep -v grep
24259 root 1752 S {testFree.sh} /bin/sh ./testFree.sh
# ps | grep testF | grep -v grep
24259 root 1752 S {testFree.sh} /bin/sh ./testFree.sh
# ps | grep testF | grep -v grep
6961 root 0 RW [testFree.sh]
24259 root 1752 R {testFree.sh} /bin/sh ./testFree.sh
# ps | grep testF | grep -v grep
24259 root 1752 S {testFree.sh} /bin/sh ./testFree.sh
# ps | grep testF | grep -v grep
9738 root 1752 S {testFree.sh} /bin/sh ./testFree.sh
24259 root 1752 S {testFree.sh} /bin/sh ./testFree.sh
========================
为什么有时会有两个 testFree.sh 在运行呢?注意观察 pid=24259 这个是本来真正运行的进程