IMHO, you are in a cul-de-sac and there is no way to do what do you want that way.
The problem is that your TIME
information is stale because, probably, the TIME
property of that processes changed while the script is running. The couple TIME
, CMD
cannot identify a process.
Why do you do awk '{print $7, $8}'
? At that point you do not need the CMD
, you need only the PID
to identify the process, so simply change it to awk '{print $7, $2}'
.
You could change your code as follows to print the PID
s of the processes you are searching for:
ps -eaf \
| awk '{print $7, $2}' \
| sort -n \
| grep -v TIME \
| grep -v 00:00:00 \
| awk -F ":" '{if ( $2 >= 01 ) print}' \
| awk '{print $2}'
In addition, your code has a subtle bug: what happens if a process has 04:00:23
as TIME
? Your code will skip it because checks only minutes field in TIME
. You can fix it this way:
ps -eaf \
| awk '{print $7, $2}' \
| sort -n \
| grep -v TIME \
| grep -v 00:00:00 \
| awk -F ":" '{if (( $1 >= 01 ) || ( $2 >= 01 )) print}' \
| awk '{print $2}'
Some suggestions to improve your skills:
- You should check the
-o
option of ps
and the -F
option of awk
and, perhaps, the meaning of TIME
column of ps
.
Try to understand how the following line works:
ps -eo time,pid --no-headers | awk -F '[ :]*' '$1>0 || $2>0 {print $4}' | xargs kill
timeout
(from GNU coreutils)? – Kusalananda Aug 08 '19 at 16:34