Sunday, July 5, 2009

Look inside the process using /proc

That's what might be useful even for web-developers, not just sysadmins and geeky geeks.

Often there is a need to check what's some process doing right now. `top` is of course showing us 100% of CPU consumption but you need to have some sort of progress to decide if you can wait or you'll just kill the task and do it in a different way.

Let's take `grep` as an example of some task that may take hours on a big set of files:

user@host [/tmp/files] $ grep -e "$PATTERN" *.dat

So to use magic you don't need patch grep or to have knowledge of how to debug Linux kernel. I use `/proc` for this task:

  1. Find PID of the grep process, like this "ps axuww | grep grep" :)
  2. Exec "ls -al /proc/$PID/fd/"
  3. You'll get directory list as an output. One of these file descriptors the a process is grep's current file it's processing right now.

lrwx------ 1 user 64 2009-07-05 04:12 0 -> /dev/pts/2
lrwx------ 1 user 64 2009-07-05 04:12 1 -> /dev/pts/2
lrwx------ 1 user 64 2009-07-05 04:12 2 -> /dev/pts/2
lr-x------ 1 user 64 2009-07-05 04:12 3 -> /tmp/files/2009-07-01_4534545435.dat


Voila!

For progress calculation you  can use "ls *.dat > list.txt" and then find which line you on right now.

No comments:

Post a Comment