Skip to content

Instantly share code, notes, and snippets.

@eddy85br
Last active February 1, 2018 20:26
Show Gist options
  • Select an option

  • Save eddy85br/eeb911b176c44641d080 to your computer and use it in GitHub Desktop.

Select an option

Save eddy85br/eeb911b176c44641d080 to your computer and use it in GitHub Desktop.
Grep Running Processes (psgrep)
#!/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=''
if [ "$#" -eq 0 ]; then
echo "Must inform at least 1 argument" && exit 1;
fi
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