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.
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...)
You should try jps it's similar to ps but only lists java processes with their real names instead of just "java". It's included in pretty much every JDK.
For just viewing system information, I usually prefer glances to htop, although it can't signal. It has a curses interface and also includes network traffic, docker containers, disk IO, and it's nicely pluggable for both output formats (it can dump to CSV at interval as a daemon or while you're running it interactively (or something like graphite/influxdb if you have those)) as well as collection mechanisms (you write some python).
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.
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.
Modern Linuxes have pgrep to help avoid the inevitable "lol, the grep process I fired will match" issue that results in having to filter that out as well with yet another program.
Yeah, I always felt having to type the extra bit was strange, like maybe grep could have a switch to say "don't find grep" but I guess having a switch for such a specific use case, grepping processes, is overkill.
Yes, I never knew that. But I saw it in another post and as the sibling post mentions it's enough to surround just the first character with the brackets...
This is why I love HN, I learn stuff I never knew I wanted to know!
Sometimes powerful tools require effort to understand.
Lsof is absolutely something you should spend some real time grokking.
In regard to ps you can memorize a few commands until something truly weird comes up since you will probably use other more specialized snapshotting and tracing tools for anything somewhat difficult to troubleshoot.
> `lsof` and `ps` have the most dense man pages I have ever tried to plow through
That's why I was so happy to stumble across pstree the other day. Even the man page is excellent! Every now and again, I'll come across a util like this that I can put into immediate action w/o lots of deciphering. pstree -a is awesome for a relative newbie like me.
lsof -i -n -P is a regular "reflex" command whenever I'm trying to figure out why a daemon I've just setup isn't responding to requests (immediately answers the questions: is it running? and, if yes, under what privs? and on what interface?).
That together with a dump of active iptables rules normally results in an immediate fix for 90% of "why can't I connect to X" problems :)
Although a bit rare in my usage, I have found basic use of lsof quite useful when needed. I haven't even tried many command line options. The options it provides really seem comprehensive. I have also found similar tools on Windows (like WhoLockMe or Process Explorer) quite useful.
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
You don't need any of those `exec`s. Yes, GNU grep has `-a`.
If you don't like the backslash-newline-pipe sequence, if you put the pipe at the end of the previous line, you don't need the backslash; but it's less obvious that the next line is operating on the output of the previous.
Multi-line arguments to sed can be a pain (good luck getting your editor to auto-indent them). Instead, you can use -e to specify multiple sed commands.
There's nothing in there that would make it not work in bash. That should work in any Bourne-family shell.
It only handles 0 or 1 arguments correctly, not > 1.
Multiple arguments could be added if you want that. I personally do not need it as I search the cmdline patterns I need without using spaces. I use dots instead. Quick and dirty.
I write 100's of these small scripts for my own use only so I have my own style, peculiar as it may be. I never need indentation because I always keep scripts short; I only use it occasionally and randomly.
I do not use -e with sed, unless I'm using branches or loops.
The execs seem superfluous but actually make a difference, at least on the UNIX I use. Try it with and without and see if you notice.
All my scripts are portable to Bash, but they're also portable to the most basic of Bourne-compatible shells too. I do not use Bash.
What shell are you using? I could see a naive shell forking twice without exec, but I don't know of a shell that does that. Exec just says "don't fork(3) before calling exec(3)", but, but inside of a pipeline like that, it shouldn't fork again anyway. I have tried it with and without.
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).