模拟linux内存占用脚本
#!/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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
上次更新: 2/17/2023, 5:59:37 PM