Last active
February 1, 2018 20:26
-
-
Save eddy85br/eeb911b176c44641d080 to your computer and use it in GitHub Desktop.
Grep Running Processes (psgrep)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
################################################################################ | |
## Similar to run: ps aux | grep "RegExp" ## | |
## Script that receives a list o keywords and search then in "ps axw" command ## | |
## and returns ps's header and processes that where found with keywords. ## | |
## Accepts grep options, like '-c' to count number of matched processes. ## | |
################################################################################ | |
if [ "$#" -eq 0 ]; then | |
echo -e "Must inform at least 1 argument.\n Use '-w' for watch and '-c' for count.\n" && exit 1; | |
fi | |
options='' | |
count='' | |
words='' | |
watch='' | |
seconds=1 | |
for arg in "$@"; do | |
if [[ $arg =~ ^- ]]; then | |
if [[ $arg =~ ^-w ]]; then | |
watch=1 | |
if [[ $arg =~ ([0-9]+) ]]; then | |
seconds=${BASH_REMATCH[1]} | |
fi | |
else | |
if [[ $arg =~ ^-c ]]; then | |
count=1 | |
fi | |
options="$options $arg" | |
fi | |
else | |
if [ -z $words ]; then | |
words="$arg" | |
else | |
words="$words|$arg" | |
fi | |
fi | |
done | |
if [ -z $watch ]; then | |
if [ -z $count ]; then | |
ps axw -o ppid,pid,user,%cpu,%mem,vsz,rss,wchan,nlwp,stime,etime,args | grep -v -e $0 -e $$ | egrep $options "(ELAPSED|$words)" --color=auto | |
else | |
ps axw -o ppid,pid,args | grep -v -e $0 -e $$ | egrep $options "($words)" --color=auto | |
fi | |
else | |
echo " ## Starting Watch ($seconds seconds) ##" | |
while [ 1 ]; do | |
echo -e "\n ## $(date)\n"; | |
ps axw -o ppid,pid,user,%cpu,%mem,vsz,rss,wchan,nlwp,stime,etime,args | grep -v -e $0 -e $$ | egrep $options "(ELAPSED|$words)" --color=auto; | |
sleep $seconds; | |
done | |
fi | |
exit 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment