-2

I want to get the PIDs of a given process name and attach to their outputs with strace. I can do this manually like so:

$ pidof foobar
1234 2345 
$ strace -p1234 -p2345 -s9999 -e write

Great! But I want to automate this from Ansible, so I need to do both these steps in Bash i.e. without the manual conversion of the two numeric PIDs to two -p arguments.

I've tried a lot of things like creating an array from the PIDs and trying to join the array with -p but once it gets this complex you need to create a function and I don't have a lot of scope for that with an Ansible one-liner.

Thanks!

oguz ismail
  • 1
  • 16
  • 47
  • 69
deed02392
  • 4,799
  • 2
  • 31
  • 48

2 Answers2

1

strace's -p accepts a list of PIDs as well, all you need is:

strace -p "$(pidof foobar)" -s 9999 -e write
oguz ismail
  • 1
  • 16
  • 47
  • 69
  • Thanks, I did check the `strace` manpage and I didn't see mention that it supported comma separated PIDs. You prompted me to check the manpage which revealed: -p "`pidof PROG`" syntax is supported." Checkout my answer which avoids the need to use the `-S` option. – deed02392 May 27 '20 at 11:54
0

strace may support pidof output in the format of:

-p "`pidof PROG`"

Following oguz's answer, I determined that the following works:

strace -p "`pidof foobar`" -s9999 -e writ
deed02392
  • 4,799
  • 2
  • 31
  • 48