0

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?
oguz ismail
  • 1
  • 16
  • 47
  • 69
D.Zou
  • 761
  • 8
  • 25
  • please edit your Q to show which OS you are using. Add the output from `uname -srv`. Good luck. – shellter Jun 26 '21 at 00:49
  • 2
    Your question is an exact demonstration of why you should not use `\`` anymore. Use `$( )` to run commands "within" commands. It will clear up some of your quoting issues. – Nic3500 Jun 26 '21 at 02:22
  • @Nic3500 I'm not sure all (or any?) ksh versions support `$( )`. – Ed Morton Jun 26 '21 at 13:59

2 Answers2

1

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.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
1

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
Walter A
  • 19,067
  • 2
  • 23
  • 43