0

Note:

I have an older version of ps without the option to use -o etimes to get the times in seconds. So the solution of list-processes-that-have-been-running-more-than-2-hours cannot be used directly here.


I want to list the process which are running for more than 3 hours. Below is the command which i am using to list the processes

Command

ps -Ao stime,etime,cmd --sort=start_time | grep "/home/mose/bin/hi.sh" | grep -v "grep"

Output

Jun06 1-03:36:53 /bin/ksh /home/mose/bin/hi.sh -j KendaEVALUATION_7 -o 220606
Jun06 1-03:36:53 /bin/ksh /home/mose/bin/hi.sh -j KendaEVALUATION_5 -o 220606
Jun06 1-03:36:53 /bin/ksh /home/mose/bin/hi.sh -j KendaEVALUATION_8 -o 220606
Jun06 1-03:36:53 /bin/ksh /home/mose/bin/hi.sh -j KendaEVALUATION_6 -o 220606
07:54 03:43:46 /bin/ksh /home/mose/bin/hi.sh -j Kenda -o 220607
08:01 03:36:54 /bin/ksh /home/mose/bin/hi.sh -j KendaWORKING_TIME_6 -o 220607
08:01 03:36:54 /bin/ksh /home/mose/bin/hi.sh -j KendaFRONTOFFICE_5 -o 220607
08:01 03:36:54 /bin/ksh /home/mose/bin/hi.sh -j KendaWORKING_TIME_4 -o 220607
08:01 03:36:54 /bin/ksh /home/mose/bin/hi.sh -j KendaWORKING_TIME_8 -o 220607
08:01 03:36:54 /bin/ksh /home/mose/bin/hi.sh -j KendaWORKING_TIME_5 -o 220607
08:01 03:36:54 /bin/ksh /home/mose/bin/hi.sh -j KendaWORKING_TIME_3 -o 220607
08:01 03:36:54 /bin/ksh /home/mose/bin/hi.sh -j KendaWORKING_TIME_2 -o 220607
08:01 03:36:54 /bin/ksh /home/mose/bin/hi.sh -j KendaWORKING_TIME_7 -o 220607
08:01 03:36:54 /bin/ksh /home/mose/bin/hi.sh -j KendaWORKING_TIME_1 -o 220607
08:30 03:07:01 /bin/ksh /home/mose/bin/hi.sh -j JituSUBT -o 220607
08:40 02:56:58 /bin/ksh /home/mose/bin/hi.sh -j JituREFRESH -o 220607
09:10 02:27:32 /bin/ksh /home/mose/bin/hi.sh -j KendaWOCAS -o 220607
09:56 01:41:33 /bin/ksh /home/mose/bin/hi.sh -j KendaBACKOFFICE_CCB_KUA_4 -o 220607

So from how can i list only the process which are running for more than 3 hours.

thanasisp
  • 7,802
  • 2
  • 26
  • 39
  • getting below error while execting the command mentioned in the above article ps -e -o "pid,etimes,command" | awk '{if($2>7200) print $0}' ERROR: Unknown user-defined format specifier "etimes". – Deep Mukherjee Jun 07 '22 at 10:03
  • Yes, you need `etime` as you have already used, not `etimes`. – terdon Jun 07 '22 at 10:11
  • Huh, same here, fair enough. But if the OP wants to keep the human readable times, then the answers there won't help. – terdon Jun 07 '22 at 10:13
  • Checking some older systems, `etimes` doesn't seem to be available in "procps version 3.2.8", but is in "procps-ng version 3.3.9". Looking at the git tags and the Debian package, 3.3.9 seems to be from around 2014 already. – ilkkachu Jun 07 '22 at 10:36
  • based on above comment (1) updated this Q to reflect the problem (2) added comment to linked post for anyone with old ps (3) retracted close here. – thanasisp Jun 07 '22 at 17:12

2 Answers2

2

If the process has been running for more than 3 hours, then either you wil have a - in the second field (because it's been running for more than a day, so you will have something like 1-03:36:53), or the first number of the second field will be 3 or greater. So, also simplifying your command a little, you can do:

ps -C hi.sh -o stime,etime,cmd --sort=start_time |
    awk '$2 ~ /-/ || $2 ~ /^(0[3-9]|[12]):[0-9]+:[0-9]+/'

Which, on your example ps output, would produce:

Jun06 1-03:36:53 /bin/ksh /home/mose/bin/hi.sh -j KendaEVALUATION_7 -o 220606
Jun06 1-03:36:53 /bin/ksh /home/mose/bin/hi.sh -j KendaEVALUATION_5 -o 220606
Jun06 1-03:36:53 /bin/ksh /home/mose/bin/hi.sh -j KendaEVALUATION_8 -o 220606
Jun06 1-03:36:53 /bin/ksh /home/mose/bin/hi.sh -j KendaEVALUATION_6 -o 220606
07:54 03:43:46 /bin/ksh /home/mose/bin/hi.sh -j Kenda -o 220607
08:01 03:36:54 /bin/ksh /home/mose/bin/hi.sh -j KendaWORKING_TIME_6 -o 220607
08:01 03:36:54 /bin/ksh /home/mose/bin/hi.sh -j KendaFRONTOFFICE_5 -o 220607
08:01 03:36:54 /bin/ksh /home/mose/bin/hi.sh -j KendaWORKING_TIME_4 -o 220607
08:01 03:36:54 /bin/ksh /home/mose/bin/hi.sh -j KendaWORKING_TIME_8 -o 220607
08:01 03:36:54 /bin/ksh /home/mose/bin/hi.sh -j KendaWORKING_TIME_5 -o 220607
08:01 03:36:54 /bin/ksh /home/mose/bin/hi.sh -j KendaWORKING_TIME_3 -o 220607
08:01 03:36:54 /bin/ksh /home/mose/bin/hi.sh -j KendaWORKING_TIME_2 -o 220607
08:01 03:36:54 /bin/ksh /home/mose/bin/hi.sh -j KendaWORKING_TIME_7 -o 220607
08:01 03:36:54 /bin/ksh /home/mose/bin/hi.sh -j KendaWORKING_TIME_1 -o 220607
08:30 03:07:01 /bin/ksh /home/mose/bin/hi.sh -j JituSUBT -o 220607

The awk command is searching for lines where the second field ($2) contains a - or where it starts with (^) either a 0 followed by a number between 3 and 9 (0[3-9]), or (|) either a 1 or a 2 ([12]), and then the line continues with two sets of numbers separated by a : ([0-9]+:[0-9]+).

terdon
  • 234,489
  • 66
  • 447
  • 667
  • Please note i do have output like below as well 11:53 23:13 /bin/ksh /home/mose/bin/hi.sh -j KendaWORKING_8 -o 220607 11:53 23:13 /bin/ksh /home/mose/bin/hi.sh -j KendaWORKING_2 -o 220607 11:53 23:13 /bin/ksh /home/mose/bin/hi.sh -j KendaWORKING_7 -o 220607. If i execute the above command it will exclude the one which are less than 3 hours but not the jobs which are running for some minutes or so. it will exclude only the hour jobs. How to exclude the ones which are running for some minutes as well – Deep Mukherjee Jun 07 '22 at 10:20
  • @DeepMukherjee please add that to your question. See updated answer. – terdon Jun 07 '22 at 10:24
  • Thank you so much it worked. Can you help me explain this one of the code please -> awk '$2 ~ /-/ || $2 ~ /^(0[3-9]|[12]):[0-9]+:[0-9]+/' – Deep Mukherjee Jun 07 '22 at 10:28
  • You're welcome @DeepMukherjee.See updated answer for the explanation. Also please see https://unix.stackexchange.com/help/someone-answers. – terdon Jun 07 '22 at 12:31
1

According to the man page, the format for the etime field is [[DD-]hh:]mm:ss, so a Perl regex like /(((\d+)-)?(\d+):)?\d+:\d+/ should work to match against it, and capture the day and hour in $3 and $4. (\d+ matches any number of digits, (...)? is an optional group and all the parenthesis capture the matching parts. The day and hour are the parts corresponding to the third and fourth opening parenthesis from the left.)

So, something like this:

ps -Ao stime,etime,cmd --sort=start_time  | 
  perl -ane '$F[1] =~ /(((\d+)-)?(\d+):)?\d+:\d+/; print if $3 > 0 || $4 >= 3'

With autosplit (-a) on, the fields of the input lines appear in the array @F. The snippet takes the second field of input, $F[1] (numbering from zero), and matches it against the regex above. Then we print the line if the number of days is greater than zero, or the number of hours is three or more. If the values weren't there, they get captured as undefined values which compare as zeroes, so e.g. 12:34 would give the same result as 00:12:34 and would not match.

ilkkachu
  • 133,243
  • 15
  • 236
  • 397