• Post author:
  • Post category:shell
  • Post comments:0评论
2

脚本内容:

#!/bin/bash
#Author: cp

if [ $# -ne 1 ] || [[ ! $1 =~ ^[0-9]+$ ]];then
    echo "Usage: $0 < CPUs >"
    exit 1;
fi

for i in `seq $1`
do
    (i=0
    while true
    do
        i=i+1
    done) &
    pid_array[$i]=$!
done

for i in ${pid_array[*]}
do
    echo "kill $i"
done


脚本解读:

命令行执行:for i in `seq 2`;do echo "i=0;while true;do i=i+1;done" | /bin/bash & done
(1)首先对传递的参数进行判断

if [ $# -ne 1 ] || [[ ! $1 =~ ^[0-9]+$ ]];then   # 当传递的参数不是一个或者不是整数时,执行下面代码
    echo "Usage: $0 <CPUs>"
    exit 1;     # 非正常运行导致退出程序。即执行退出后使用"echo $?"查看脚本执行返回码看到的是1,而不是0。
fi

(2)死循环

for i in `seq $1`      # 使用for循环从序列中提取值赋值给变量i,有几个值就会循环几次,即产生几个后台运行的死循环
do
    (i=0
    while true         # 条件永远为真,一直陷入循环,
    do
        i=i+1          # 不停计算i=i+1
    done) &            # 后台运行
    pid_array[$i]=$!   # 定义数组,$!表示最后后台运行进程的PID,将死循环进程PID记录到数组中
done

(3)打印进程号,方便kill

for i in ${pid_array[*]}    # 利用for循环从数组中取值,逐行打印
do
    echo "kill $i"
done


脚本效果:

[root@controller ~]# bash killcpu.sh 16
kill 18818
kill 18820
kill 18822
kill 18824
kill 18826
kill 18828
kill 18830
kill 18832
kill 18834
kill 18836
kill 18838
kill 18840
kill 18842
kill 18844
kill 18846
kill 18848
[root@controller ~]# top 
top - 18:40:04 up  4:20,  3 users,  load average: 5.98, 3.43, 2.45
Tasks: 534 total,  20 running, 514 sleeping,   0 stopped,   0 zombie
%Cpu0  : 99.3 us,  0.7 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  : 98.7 us,  1.3 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  : 98.7 us,  1.3 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  : 98.7 us,  1.3 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu4  : 99.7 us,  0.3 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu5  : 98.4 us,  1.6 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu6  : 96.4 us,  3.6 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu7  : 99.0 us,  1.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu8  : 99.7 us,  0.3 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu9  : 99.7 us,  0.3 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu10 : 99.3 us,  0.7 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu11 :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu12 : 99.0 us,  1.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu13 : 99.7 us,  0.3 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu14 : 99.3 us,  0.7 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu15 : 99.7 us,  0.3 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
[root@controller ~]# uptime
 18:40:31 up  4:21,  3 users,  load average: 10.40, 4.66, 2.89
[root@controller ~]# kill 18818
[root@controller ~]# kill 18820
[root@controller ~]# kill 18822
[root@controller ~]# kill 18824
[root@controller ~]# kill 18826
[root@controller ~]# kill 18828
[root@controller ~]# kill 18830
[root@controller ~]# kill 18832
[root@controller ~]# kill 18834
[root@controller ~]# kill 18836
[root@controller ~]# kill 18838
[root@controller ~]# kill 18840
[root@controller ~]# kill 18842
[root@controller ~]# kill 18844
[root@controller ~]# kill 18846
[root@controller ~]# kill 18848
2

发表回复

验证码: 5 + 5 =