[unix] Determine who is monopolizing the CPU
$ ps -eo pcpu,size:8,user,start_time,pid,args | sort -nk 1 -r |egrep -v "COMMAND|egrep -v|start_time"| head -10
The ps command displays every process (-e) with user-defined format (e.g., -o pcpu).
pcpu = CPU usage percentage
size = memory size in kB (with 8 character long field — otherwise the formatting is messed up!)
user = user name
start_time = start time
pid = process ID,
args = command being run
The sort command sorts the lines numerically (-n) in the reverse order (-r) based on the first field (-k 1).
Then egrep -v filters the lines that do not match the header (containing COMMAND), egrep and ps commands themselves.
The head -10 command filters out top 10 lines from the list generated by previous sort. (Note: here -10 is actually redundant since head command prints out top 10 lines by default, but it is there as a reminder that we can print more lines using a number greater than 10 it if there is a need!)
Credit: Nixcraft.
[osx] Eject a volume ‘unwilling’ to be ejected!
I sometime have problem finding which persistent process is not letting me eject a volume (CD/DVD/USB drive). The error message is not very helpful at all:
Try quitting applications
OK, but which one(s)? I often have hard time figuring that out using htop/ top or a combination of ps and grep.
But actually this is what I want:
$ lsof | grep -i Volume_Namewhere Volume_Name is the name of the volume (or an ‘identifying’ part of the name) I want to eject. This gives me the name(s) and process ID(s) of the run-away process(es) as well as the path to the file(s) in use on the volume, Volume_Name.
Now that I know the names I should first try to save the documents and quit the applications involved in the normal way. However, if a particular application does not oblige, I can always kill it (with the risk of potential data loss!) using its process ID (say, 123):
$ kill -9 123After that, the ejection of the volume should not be difficult at all.
Needless to say, being a Unix utility, lsof (=“list open files”) may be used on other *nix-based systems as well.
[awk] Print selective columns and watch for changes
Print columns 1, 5 and then 2, put a colon, and then print column 10 of a line that matches pattern from the file columns.txt:
$ awk '/pattern/ {print $1, $5, $2, ": ", $10}' columns.txt
.
Protecting the special meaning of the single quotes and curly braces
I came across this situation when I was trying to watch the above command as well as two other commands (say, command1 and command2) at the same time. The correct way to do that is by properly protecting the meaning of the special characters in the above awk invocation:
$ watch -d -n 20 "command1; command2; awk '"'/pattern/ {print $1, $5, $2, ": ", $10}'"' columns.txt".
N.B. The “-d”flag highlights the changes, whereas the “-n 20” flag causes the above to watch every 20 seconds.
Just for the heck of it, let me put the full command that I was actually using:
$ watch -d -n 20 "ps aux | grep -i columbus | grep -v grep && echo; tail WORK/ciudgsm && echo && grep bond output.log | tail -13 && echo && awk '"'/state # 1/ {print $3,$4,$5,": ", $10}'"' output.log && echo; awk '"'/state # 2/ {print $3,$4,$5,": ", $10}'"' output.log && head curr_iter"
(suggestions for making the above shorter, other than by aliasing, are most welcome!).
Reference: here.

leave a comment