自动杀掉占用较多CPU资源的Shell脚本
#!/bin/bash
#March-13-2006 #CPUusetriggerscriptbyNoel # #bashcodetowatcharunningprogram'sCPUusage. #ifit'saboveasetvalue,itwillautosendanemail. #YouwillneedtosetaCronjobtorunthisscripteveryxxminutes # #Setsomeneededthings: # processToWatch="convert"#inmycaseIneedtowatchconvert emailAddress="root@host"#thisismymainemailaddress triggerValue=90#iftheCPUuseisabove90%sendanemail.DONOTUSEaDOTorCOMMA! tempFileName=tmp-cpu#somenameofthetempfilefortheps,grepdata
psauxww|grep"$processToWatch"|grep-vgrep>/tmp/$tempFileName exportLINE ( readLINE while[-n"$LINE"] do set$LINE readLINE if[$(echo"$3"|sed-e's/\.[0-9]*//g')-gt$triggerValue];then mail-s"CPUmessagealertfor:$processToWatch"$emailAddress<<-END Thisistoinformyouthatthefollowingprocess:$processToWatchwithPID(ProcessID)$2isnowusingmorethanyourpreset$triggerValuevalue.
Process:$processToWatchisusing:$3ofCPUpower! Thecommandusedis:$11 END fi done )</tmp/$tempFileName