Showing posts with label unix. Show all posts
Showing posts with label unix. Show all posts

Wednesday, September 21, 2011

Getting file position using /proc

As I wrote in my post of 2009, "Often there is a need to check what's some process doing right now". That time I discovered this simple way of checking which file a process(e.g. grep) was using.

Another trick of this kind is to get a file position. This is a very common scenario. For example, you want to know the progress of unpacking of a really huge .gz file. You cannot just estimate this by analyzing the size of the data already unpacked.

Getting file position is as easy as getting a name of an open file. Thanks to Miklos Szeredi starting from Linux-2.6.22 this info can be obtained from /proc/PID/fdinfo/FD:

$ python
>>> f=open('/home/turist/work/big.txt')
>>> f.seek(1234)

(in another terminal, 3580 is python's PID)
$ ls -al /proc/3580/fd/
total 0
dr-x------ 2 turist turist  0 2011-09-21 14:40 .
dr-xr-xr-x 7 turist turist  0 2011-09-21 14:40 ..
lrwx------ 1 turist turist 64 2011-09-21 14:40 0 -> /dev/pts/1
lrwx------ 1 turist turist 64 2011-09-21 14:40 1 -> /dev/pts/1
lrwx------ 1 turist turist 64 2011-09-21 14:40 2 -> /dev/pts/1
lr-x------ 1 turist turist 64 2011-09-21 14:40 3 -> /home/yevgen/work/big.txt

(so 3 is our file descriptor, we will use it now)

$ cat /proc/3580/fdinfo/3
pos:	1234
flags:	0100000
Easy!

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.

Thursday, July 17, 2008

Fring for Maemo

Hi all!

Great news - Fring is now available for maemo platform. Fring is an application that combines a number of protocols such as Skype, ICQ, GTalk into one handy program. You may find it following this link. Here is the screenshot I took from Fring's russian blog. It should work for both chinook and diablo. The announce says about N810, but I'll give it a try tomorrow on my N800.

Tuesday, April 1, 2008

Bash: top keyboard shortcuts

There are people around me who've been Linux developers for years and they haven't taken a labor of reading "man bash" so far. It will not of course give you 1000% performance increase immediately but is worth reading it at least once. Just to support popular tradition, here is my list of most useful and frequently used keyboard shortcuts in bash(in order of popularity):

  1. Tab - autocomplete current command, directory or file name. I think it's about 50% of the whole statistics :)
  2. Ctrl-C - kill currently running something.
  3. Ctrl-R - search command history backward.
  4. Ctrl-L - the same as `clear` command. Wipe the screen. Very cool when you need to have blink console for the next info.
  5. Ctrl-A/Ctrl-E - go to the beginning/end of the current line. Sometimes Home/End keys are broken. Just in case.
  6. Ctrl-U/Ctrl-K - remove text from current position to the beginning or from current position to the end. Of course you can press <- or Delete key 200 times to get the same result..
  7. Ctrl-W - Remove the word(or part of it) before current position.
And this is something I've learnt recently:
  1. M-f/M-f - just forward/back one word
  2. Ctrl-v Tab - insert "Tab" char. Previously I did copy from Vim and paste :(
  3. M-u/M-l - change the current word to upper/lower case. Especially useful when Caps was pressed accidentally :)
  4. Ctrl-x @ - auto-complete hostname