ksh cmd one-liner to grep for several PIDs at once

154 Views Asked by At

I got a bunch of processes that I need to check CPU affinity for, so I got this one liner:

for i in `ps -Fae | grep proc_name| awk '{print $2}'`; do taskset -acp $i;done

but I have a problem, taskset shows all the child processes' pid too so I get a massive line of numbers along with their cpu affinity.

I want to pipe the above line into an egrep 'pid1|pid2' so I can filter out all the child processes.

I tried to this: for i in `ps -Fae | grep proc_name| awk '{print $2}'`; do taskset -acp $i;done | xargs egrep 'ps -Fae | grep proc_name| awk '{print $2}''

but my ksh shell didn't like the awk brackets at all.

So I have two questions:

  1. can taskset be changed to show only parent pid?
  2. how do I write the last bit where I egrep only the parent pid?
2

There are 2 best solutions below

0
On BEST ANSWER

Filter inside the loop:

for i in $(ps -Fae | grep proc_name| grep -v grep | awk '{print $2}'); do
   taskset -acp "$i" | grep "$i"
done
0
On

It sounds like you're asking for this syntax if it were bash (see https://mywiki.wooledge.org/BashFAQ/001, I'm not sure what the equivalent robust read loop syntax is for ksh):

while IFS= read -r i; do
    taskset -acp "$i"
done < <(ps -Fae | awk '/proc_name/{print $2}') |
grep -E 'pid1|pid2'

but that's pretty fragile, e.g. if pid1 appeared as a substring of some other pid. If you edit your question to provide concise, testable sample input (i.e. the output of ps -Fae and the associated output of taskset) plus the expected output then we can be of more help.