消耗CPU和内存的脚本
用法
./shell.sh 4 (4为4内核)
查看cpu内核数量 > lscpu
执行后会出现一堆kill命令,方便kill掉进程
- #!/bin/bash
- endless_loop()
- {
- echo -ne "i=0;
- while true
- do
- i=i+100;
- i=100done" | /bin/bash &
- }
- if [ $# != 1 ]; then
- echo "USAGE: $0 <cpus>"
- exit 1;
- fi
- for i in `seq $1`
- do
- endless_loop
- pid_array[$i]=$!;
- done
- for i in "${pid_array[@]}"; do
- echo 'kill' $i ';';
- done
shell消耗内存
以前有过[shell消耗cpu][1]的文章,再记录一个shell消耗内存代码。
- #!/bin/bash
- mkdir /tmp/memory
- mount -t tmpfs -o size=1024M tmpfs /tmp/memory
- dd if=/dev/zero of=/tmp/memory/block
- sleep 3600
- rm /tmp/memory/block
- umount /tmp/memory
- rmdir /tmp/memory
用tmpfs文件系统占用内存一个小时