Neeraj
Neeraj

Reputation: 1173

Kill all Processes on Unix running older than 10 days as per a pattern matching

I have a typical case where I have to kill all the processes which are opened and running older than a particular day.

The command I have drafted is somewhat like this

ps -eo etime=,pid=,comm=,user=|**awk '/^[0-9]/** && /abcadmin/ {print $2}'|xargs -I{} kill {}

The output is as below on which the awk filtering happens.

35-23:07:24 3227762 oracl     oracl<br/>
 2-23:07:51 3231800 oracl     oracl<br/>
 7-02:32:20 3235868 oracl     oracl<br/>
   22:59 :52 3240084 oracl     oracl<br/>
69-01:37:23 3244086 oracl     oracl<br/>
 5-23:29:34 3248352 oracl     oracl<br/>
 **5-23:29:34 3268752 abc     abcadmin<br/>
40-21:41:05 3272841 abc       abcadmin<br/>
11-21:41:05 3272842 abc       abcadmin**<br/>
 1-19:48:25 3301506 oracl     oracl<br/>
70-02:00:11 3305648 oracl     oracl<br/>
 2-02:17:39 3321950 oracl     oracl<br/>
      34:47 3326122 sshd         root<br/>

I am trying to script the following logic:

  1. Take the first column under awk
    e.g. `1-19:48:25

  2. Take characters before the first hyphen.
    e.g. in this case it's 1

  3. Check if it's greater than 10 ,
  4. If yes then print the pids
  5. Delete the pids.

I don't know how to do step 2 & 3 inside awk command. I'm new to awk scripting; any help is appreciated.

Upvotes: 0

Views: 2290

Answers (1)

Matthew Cline
Matthew Cline

Reputation: 2376

An easier way to do it is to use the --older-than option of killall:

Match only processes that are older (started before) the time specified. The time is specified as a float then a unit. The units are s,m,h,d,w,M,y for seconds, minutes, hours, days, weeks, Months and years respectively.

Upvotes: 1

Related Questions