Skip to content

Instantly share code, notes, and snippets.

@aramisf
Forked from eddy85br/psgrep.sh
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save aramisf/b18168c80f5114c36300 to your computer and use it in GitHub Desktop.

Select an option

Save aramisf/b18168c80f5114c36300 to your computer and use it in GitHub Desktop.
#!/bin/bash
################################################################################
## Similar to run: ps aux | grep "RegExp" ##
## Script that receives a list o keywords and search then in "ps aux" command ##
## and returns ps's header and processes that where found with keywords. ##
## Accepts grep options, like '-c' to count number of matched processes. ##
################################################################################
options=''
words=''
for arg in "$@"
do
if [[ $arg =~ ^- ]]; then
options="$options $arg"
else
if [ -z $words ]; then
words="$arg"
else
words="$words|$arg"
fi
fi
done
if [ -z $options ]; then
ps axw -o ppid,pid,user,%cpu,%mem,vsz,rss,wchan,nlwp,stime,etime,args | grep -v -e $0 -e $$ | egrep "(ELAPSED|$words)" --color=auto
else
ps axw -o ppid,pid,args | grep -v -e $0 -e $$ | egrep $options "($words)" --color=auto
fi
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment