3

When I run pgrep vim | xargs echo, it prints 93868 91234.

When I run lsof -p 91234, it prints:

COMMAND   PID USER   FD   TYPE DEVICE  SIZE/OFF    NODE NAME
vim     91234 rose  cwd    DIR    1,2      1326 1186863 /Users/rose/spotapi/spotapi/models
vim     91234 rose  txt    REG    1,2   1629296   12231 /usr/bin/vim
vim     91234 rose  txt    REG    1,2   2704416  294199 /System/Library/Frameworks/Python.framework/Versions/2.7/Python

Why then does

pgrep vim | xargs lsof -p 

print

lsof: status error on 91234: No such file or directory

I'm running on Mac OS.

Rose Perrone
  • 61,572
  • 58
  • 208
  • 243
  • cannot replicate this error on a linux, maybe it's [this issue](http://stackoverflow.com/questions/12468629/lsof-should-give-all-open-files-for-a-set-of-pids)? or does [lsof -c](http://stackoverflow.com/a/12468802/2399627) help? – ultima_rat0 Sep 15 '13 at 22:03

2 Answers2

5

I see this on my Slackware installation. If you run lsof -p 93868 91234 you will see the problem. The reason is that xargs shoves all the arguments onto one command line if it can. You can use the -n option to specify the maximum number of arguments.

pgrep vim | xargs -n1 lsof -p
paddy
  • 60,864
  • 6
  • 61
  • 103
0

Alternatively, you can supply multiple PIDs by replacing spaces with commas:

lsof -p `pgrep vim | xargs echo | sed -re 's/ /,/g'`

In the example above, I use xargs to pass all pids to echo as a way of getting them into a single line, and then replace spaces with commas. The executed command against lsof is:

lsof -p 1111,2222

An easier way of doing this (if you know the process name, e.g. vim) and don't need pgrep:

lsof -p `pidof vim | sed -re 's/ /,/g'`

Not 100% sure of compatibility and availability of this in osx, this was a Fedora / Ubuntu.

Brett
  • 5,690
  • 6
  • 36
  • 63