Live data from Hacker News

A Unix Utility to Know About: lsof (2009)

catonmat.net

21–30 of 54 posts

Re: A Unix Utility to Know About: lsof (2009)

#21
post #2

`lsof` and `ps` have the most dense man pages I have ever tried to plow through, and that's causing me to use these tools only at a minimum.

I am guilty of using `ps|aux` for the sole purpose of answering this question: Is it (still) running ? edit: `ps aux|grep`, indeed. Silly me in the early hours of a rest day :).

>> I am guilty of using `ps|aux`

What's wrong with doing that?

I've been doing that for so long I don't know why I do it

Re: A Unix Utility to Know About: lsof (2009)

#22

Earlier quoted context omitted.

For interactive use, I strongly recommend htop if you're still doing stuff like "ps aux | grep" and then "kill 84728". Htop is top on steroids: ncurses-based and very fast, has colors, threaded or flat process view, sorting by different criteria like cpu or ram use, searching for substring match in process name, interface to killing/signalling to selected process, etc. http://hisham.hm/htop/

I am more a `killall ` guy.

Don't do that on Solaris ;-)

Re: A Unix Utility to Know About: lsof (2009)

#23
post #5

For finding what uses a file, I'm an 'fuser' kid but lsof is fine too. Using posh right now, so had to make my own: function fuser($relativeFile){ $file = Resolve-Path $relativeFile foreach ( $Process in (Get-Process)) { foreach ( $Module in $Process.Modules) { if ( $Module.FileName -like "$file*" ) { $Process | select id, path } } } } In use: > fuser .\node_modules\ Id Path -- ---- 2660 C:\Program Files\nodejs\node.…

I find myself using lsof for it's ability to show me what TCP sockets are in use more than what actual files are open.

lsof | grep TCP

Re: A Unix Utility to Know About: lsof (2009)

#24
post #22

Earlier quoted context omitted.

I am more a `killall ` guy.

Don't do that on Solaris ;-)

Been there, done that :(

See also the 'reboot' (or is it the 'poweroff' ?) command, which is much more direct than the Linux equivalent...

Re: A Unix Utility to Know About: lsof (2009)

#25

Earlier quoted context omitted.

For interactive use, I strongly recommend htop if you're still doing stuff like "ps aux | grep" and then "kill 84728". Htop is top on steroids: ncurses-based and very fast, has colors, threaded or flat process view, sorting by different criteria like cpu or ram use, searching for substring match in process name, interface to killing/signalling to selected process, etc. http://hisham.hm/htop/

I am more a `killall ` guy.

Sometimes, I want something a little more murderous, so I tend to write this script on most systems I use:

    ps -ef | grep $1 | awk '{print "kill -9 " $2}' | sh
Usually stored as /usr/local/bin/kll

I've found it's very useful for killing specific Java programs without having to killall -9 java.

(and, yeah, I should probably be using ps -efww instead of ps -ef, but old habits die hard... I've already got ps aliased to ps -ww for interactive use, so I should probably change this script...)

Re: A Unix Utility to Know About: lsof (2009)

#26
post #10

Earlier quoted context omitted.

You mean `ps aux | grep` which I am also guilty of using every time ? You might be interested in pgrep ( http://linux.die.net/man/1/pgrep ) as well.

For interactive use, I strongly recommend htop if you're still doing stuff like "ps aux | grep" and then "kill 84728". Htop is top on steroids: ncurses-based and very fast, has colors, threaded or flat process view, sorting by different criteria like cpu or ram use, searching for substring match in process name, interface to killing/signalling to selected process, etc. http://hisham.hm/htop/

I discovered htop when regular top changed its default behavior to be nigh unusable. htop was recommended as an alternative, and while I've since learned how to configure top to go back to its old behavior [0], I've found that I just prefer htop now.

[0] https://bbs.archlinux.org/viewtopic.php?id=189757

Re: A Unix Utility to Know About: lsof (2009)

#27
lsof is my go-to command for "why won't this drive unmount?". That alone makes it incredibly useful.

Also, back in the bad old days, 'lsof | grep snd' helped track down what the hell was hogging my sound card (setting up proper mixing has made that a distant memory, though).

Re: A Unix Utility to Know About: lsof (2009)

#28
post #2

`lsof` and `ps` have the most dense man pages I have ever tried to plow through, and that's causing me to use these tools only at a minimum.

I am guilty of using `ps|aux` for the sole purpose of answering this question: Is it (still) running ? edit: `ps aux|grep`, indeed. Silly me in the early hours of a rest day :).

I have this little bash function which I like to use to find out if a program is running, and to optionally kill it. Originally my goal was to keep it fitting inside a tweet, but adding helpful messaging just pushed it over.

    pgk ()
    {
        [ -z "$*" ] && echo 'Usage: pgk ' && return 1;
        pgrep -fl $*;
        [ "$?" == "1" ] && echo 'No processes match' && return 1;
        echo 'Hit [Enter] to pkill, [Ctrl+C] to abort';
        read && pkill -f $*
    }

Re: A Unix Utility to Know About: lsof (2009)

#30
s/Unix/Linux/

The following can be adapted to provide other information: whatever procfs provides. This is a rough equivalent of "pgrep -fl .|less". Work-in-progress. Don't know if Linux grep has "-a" option.

  #! /bin/sh 
  # Almquist clone, not Bash
 
  case $# in
  0)
  exec grep -a . proc/[0-9]*/cmdline \
  |exec tr '\000' '\040' \
  |exec sed '
             /grep -a .* proc/d;
             #parent: '"$$"';
             s/proc./ /;
             s/\/cmdline:/ /;
 ' \
  |exec less
  ;;
 
  *)
  exec grep -a . proc/[0-9]*/cmdline \
  |exec tr '\000' '\040' \
  |exec grep $@ \
  |exec sed '
             /grep '"$@"'/d;
             s/proc./ /;
             s/\/cmdline:/ /;
 ' \
  |exec less
  esac
Post reply on HN