zl程序教程

您现在的位置是:首页 >  系统

当前栏目

模拟linux内存占用脚本

Linux内存模拟 脚本 占用
2023-06-13 09:18:39 时间
#!/bin/bash
################################################################
#       Mem Used Script
#       eg. ./mem.sh 10G & to start testing
#       eg. ./mem.sh stop  to stop testing and clear env
#       update: 2020-04-21  charseki
################################################################
num=$1
user=`whoami`
 
start()
{
if [ -d /tmp/memory ];then
        echo "the dir "/tmp/memory" is already exist!, use it." >> mem.log
else
        sudo mkdir /tmp/memory
        mount -t tmpfs -o size=$num tmpfs /tmp/memory
fi
dd if=/dev/zero of=/tmp/memory/block >> mem.log 2>&1
}
 
stop()
{
 
rm -rf /tmp/memory/block
umount /tmp/memory
rmdir /tmp/memory
if [ -d /tmp/memory ];then
        echo "Do not remove the dir \"/tmp/memory\", please check "
else
        echo "clear env is done!"
fi
}
main()
{
if [ $num == 'stop' ];then
        stop
elif [ $user != "root" ];then
        echo "please use the \"root\" excute script!"
        exit 1
else
        start
fi
}
 
if [ $# = 2 -o $# = 1 ];then
        main
else
        echo 'Usage: <./mem.sh 10G &> to start  or <./mem.sh stop>  to clear env'
fi