Function that searchs a process by its name and shows in the terminal. * Shows the Header for reference * Hides the process 'grep' from the list * Case sensitive Show Sample Output
Note that `grep "$(ir foo)"` really doesn't save any typing, but wrapping this inside a second shell function will:
psg() { grep "$(ir \"$@\")" ;}
Show Sample Output
The '[r]' is to avoid grep from grepping itself. (interchange 'r' by the appropriate letter)
Here is an example that I use a lot (as root or halt will not work):
while (ps -ef | grep [w]get); do sleep 10; done; sleep 60; halt
I add the 'sleep 60' command just in case something went wrong; so that I have time to cancel.
Very useful if you are going to bed while downloading something and do not want your computer running all night.
You'll need to install proctools. MacPorts and Fink have this if you're running Mac OS X, check your Linux distribution's repositories if it isn't installed by default. Show Sample Output
hb(){ sed "s/\($*\)/`tput setaf 2;tput setab 0;tput blink`\1`tput sgr0`/gI"; }
hb blinks, hc does a reverse color with background.. both very nice.
hc(){ sed "s/\($*\)/`tput setaf 0;tput setab 6`\1`tput sgr0`/gI"; }
Run this:
command ps -Hacl -F S -A f | hc ".*$PPID.*" | hb ".*$$.*"
Your welcome ;)
From my bash profile - http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html
Show Sample Output
Function that searchs for process by its name:
* Shows the Header for reference
* Hides the process 'grep' from the list
* Case sensitive
The typical problem with using "ps | grep" is that the grep process shows up the in the output.
The usual solution is to search for "[p]attern" instead of "pattern".
This function turns the parameter into just such a [p]attern.
${1:0:1} is the first character of $1
.
${1:1} is characters 2-end of $1
Show Sample Output
Grep for a named process. Show Sample Output
David thanks for that grep inside! here is mine version: psgrep() { case ${1} in ( -E | -e ) local EXTENDED_REGEXP=1 shift 1 ;; *) local EXTENDED_REGEXP=0 ;; esac if [[ -z ${*} ]] then echo "psgrep - grep for process(es) by keyword" >&2 echo "Usage: psgrep [-E|-e] ... " >&2 echo "" >&2 echo "option [-E|-e] enables full extended regexp support" >&2 echo "without [-E|-e] plain strings are looked for" >&2 return 1 fi \ps -eo 'user,pid,pcpu,command' w | head -n1 local ARG='' if (( ${EXTENDED_REGEXP} == 0 )) then while (( ${#} > 0 )) do ARG="${1}" shift 1 local STRING=${ARG} local LENGTH=$(expr length ${STRING}) local FIRSCHAR=$(echo $(expr substr ${STRING} 1 1)) local REST=$(echo $(expr substr ${STRING} 2 ${LENGTH})) \ps -eo 'user,pid,pcpu,command' w | grep "[${FIRSCHAR}]${REST}" done else \ps -eo 'user,pid,pcpu,command' w | grep -iE "(${*})" fi }
commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.
Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.
» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10
Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):
Subscribe to the feed for: