korn shell script guidance

Please look over my short comings during this line if questions.

I have multiple lines in a file that look like,

phycook 618722      1  76 12:15:13 pts/122 70:24 /4js/runtime2.02.10/lib/fglrun-bin initmenu.42r
 phycook 593484      1  65 12:15:18 pts/122 69:22 /4js/runtime2.02.10/lib/fglrun-bin initmenu.42r

I was thinking I could put this in a for loop like so.

for 1 2 3 4 in $(cat outfile)
do 
echo $4
done

Since there are spaces between each value in each line that would set the value in the first column for line 1 to $1 and would set the value in the 2nd column for line 1 to #2 and so on. Then I could check $4 for a certain value and if true then the next step would be to kill the process which would be in $2.

Please don't laugh. I somehow think I remember doing something like this but can't find the freaking code in another script.

So if I didn't do a good job of explaining above I basically want to take the value in 4th column and if it's above a certain number run a kill against the PID which is the value in column #2.

Thank you for your time.

for the below code, it will check whether the 4th column is greater than 50. If it is, then it will issue a kill command for the processid (2nd column)

i included echo in the below code, just exeucte it and check whether it gives the correct output (as u expect). If yes, then remove the echo part.

 
awk '{if($4 > 50) print "echo kill -9 " $2}' inputfile| sh

WOW itkamaraj,

Maybe that's not big to everyone but it's hugh to me. Thank you very much. I dream of the day I have the "knowledge" power. I appreciate your quick response.

It did work as expected but if you have time would you consider adjusting to check for the 7th column (70:24) and if it is above 05:00 then kill the processid? Meaning if the value in column 4 and the value in column 7 are both true then kill the processid.

 
awk '{if($4 > 50 && substr($7,1,index($7,":")-1) > 5) print "echo kill -9 " $2}' inputfile | sh

---------- Post updated at 08:12 AM ---------- Previous update was at 08:00 AM ----------

Added one mroe condition for checking the minutes also.

 
awk '{if($4 > 50 && substr($7,1,index($7,":")-1) >= 5 && substr($7,index($7,":")+1,length($7)) > 0 ) print "echo kill -9 " $2}' test
1 Like