Our purpose is finding old process then kill them. Now I will explain below script.Mainly we have for loop and our variables are process names(that we want to kill).
Then in for loop we define the process life duration. After that we are checking there is any process that we defined. Then if exist, we will kill them then sending e-mail to admins.
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 |
########## ########## #/!bin/bash #title :processkiller.sh #description :This script kills the processes named sxmnmain(older than 3 days), rxmnmain-oxmnmain((older than 5 days)) #author :Fatih Murat Topaloglu #note :It is very easy to monitor new processes. you just need to add process name to for loop and add an elif statement to define proces life duration cd /home/remotekernel/scripts for process in sxmnmain rxmnmain oxmnmain do #define proces life duration if [ "$process" == "sxmnmain" ];then killday='3' else killday='5' fi <!--more--> psexist=$(ps -o pid,etime,command ax | grep $process | grep - | cut -c1-9 | sed 's/-//' | awk -v kill_day="$killday" '{ printf("%s %s\n",$0,$2>=kill_day?"OK":"ERR" ); }' |grep OK|awk '{print $1}' |xargs ps -f -p 2>/dev/null) if [ ${#psexist} -gt 0 ];then echo "###########"$process"##########" | awk '{print toupper($0)}' >> killed.log ps -o pid,etime,command ax | grep $process | grep - | cut -c1-9 | sed 's/-//' | awk -v kill_day="$killday" '{ printf("%s %s\n",$0,$2>=kill_day?"OK":"ERR" ); }' |grep OK|awk '{print $1}' |xargs ps -f -p >>killed.log 2>/dev/null ps -o pid,etime,command ax | grep $process | grep - | cut -c1-9 | sed 's/-//' | awk -v kill_day="$killday" '{ printf("%s %s\n",$0,$2>=kill_day?"OK":"ERR" ); }' |grep OK|awk '{print $1}' |xargs kill -9 2>/dev/null fi done cat note.txt >> mainbody.log echo " " >> mainbody.log echo $(date) >> mainbody.log echo " " >> mainbody.log echo "==========" "HOSTNAME" : $HOSTNAME "==========" | awk '{print toupper($0)}' >> mainbody.log echo " " >> mainbody.log killed_process=$(cat killed.log) if [ ${#killed_process} -gt 0 ];then cat killed.log >> mainbody.log /home/remotekernel/sendEmail/./sendEmail -s SMTP-Server-address -f "LINUX ADMINS <admin@remotekernel.com>" -u "Process Killer $HOSTNAME" -t fmt344@remotekernel.com -t fmt@remotekernel.com -cc admin@remotekernel.com -o message-file=/home/remotekernel/scripts/mainbody.log :>killed.log :>mainbody.log |
You can use SendEmail app for sending your alerts. You can find it below link.