13

If I have a piped command such as

   cat myfile | processor_application

where processor_application is something reading from standard in, in chunks, to process, is it possible to see how far through the file cat has got?

Possibly using lsof?

Thanks!

wodow
  • 590
  • 1
  • 5
  • 18
  • Thank you for the answers so far! However, I should have been clearer: the command is already running. – wodow Jan 31 '11 at 17:08
  • Ah yes, then pv is not so handy. You may want to edit your original question to make that more clear. – Phil Hollenback Jan 31 '11 at 17:14
  • Actually, there are three great separate answers in this question now, so I will leave it as it is. – wodow Jan 31 '11 at 18:04
  • 1
    Summarising, they are: 1. Use pv (before you start) 2. Use lsof -o (if offsets are supported) 3. look at value of wchar on /proc//io , for the known PID of the cat process – wodow Jan 31 '11 at 18:05

3 Answers3

13

You can use pv to do this e.g.

pv file | processor_application

As pv passes it's stdin directly to it's stdout you don't need to use cat.

Edit As your program is already running then find the PID of the cat process and then look at the contents of

/proc/<PID>/io

which will tell you how many bytes it has written - wchar.

user9517
  • 114,104
  • 20
  • 206
  • 289
  • I only just saw your edit! This works perfectly given the absence of (i) pv in the chain, (ii) lsof -o working. Thanks! – wodow Jan 31 '11 at 18:03
9

Absolutely! Pipe Viewer does exactlty that. Just insert it in your pipeline:

cat myfile | pv | processor_application

You can optimize away the cat in the above example:

pv myfile | processor_application

Which has the advantage of providing an actual progress indicator, since pv can determine the size of the input directly. If you do use pv in the middle of a pipeline, you need to supply the file size yourself to get accurate progress:

input_process | pv -s 100M -p | processor_application

Check the website for more options to customize pv.

Phil Hollenback
  • 14,647
  • 4
  • 34
  • 51
6

If the process is already running lsof has a size/offset column which may be helpful to you -- find the PID of the cat process you want to inspect and then lsof -o -p [PID].

If the process is not running yet, pv as others suggested is a good option (assuming your system has that utility).

voretaq7
  • 79,345
  • 17
  • 128
  • 213
  • This is the lsof option that I had looked at. But: what if lsof -o is not allowed or unavailable on the system? – wodow Jan 31 '11 at 17:09
  • If `-o` doesn't work on your system I think you're pretty much screwed with respect to using `lsof` :-/ (I assume you're running lsof as the user who launched the `cat` process, or as root?) – voretaq7 Jan 31 '11 at 17:14
  • Yes, tried both. I am searching for reasons lsof warns -o as not available now... – wodow Jan 31 '11 at 17:15
  • The lsof FAQ, mirrored at http://gd.tuwien.ac.at/utils/admin-tools/lsof/FAQ, implies in point/question 3.43.2.3 that lsof -o is not possible on Linux! I assume this is over-broad, as other users appear to be using lsof -o – wodow Jan 31 '11 at 17:26