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.

Revisions

  1. eddy85br revised this gist Feb 1, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion psgrep.sh
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    ################################################################################
    ## Similar to run: ps aux | grep "RegExp" ##
    ## Script that receives a list o keywords and search then in "ps aux" command ##
    ## 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. ##
    ################################################################################
  2. eddy85br revised this gist Jan 18, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion psgrep.sh
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,7 @@ else
    echo " ## Starting Watch ($seconds seconds) ##"
    while [ 1 ]; do
    echo -e "\n ## $(date)\n";
    ps axw -o ppid,pid,args,%cpu,%mem,vsz,rss,wchan,nlwp,stime,etime,args | grep -v -e $0 -e $$ | egrep $options "(ELAPSED|$words)" --color=auto;
    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
  3. eddy85br revised this gist Jan 17, 2018. 1 changed file with 33 additions and 12 deletions.
    45 changes: 33 additions & 12 deletions psgrep.sh
    Original file line number Diff line number Diff line change
    @@ -6,18 +6,30 @@
    ## 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;
    echo -e "Must inform at least 1 argument.\n Use '-w' for watch and '-c' for count.\n" && exit 1;
    fi

    for arg in "$@"
    do
    options=''
    count=''
    words=''
    watch=''
    seconds=1

    for arg in "$@"; do
    if [[ $arg =~ ^- ]]; then
    options="$options $arg"
    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"
    @@ -26,11 +38,20 @@ do
    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

    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
    ps axw -o ppid,pid,args | grep -v -e $0 -e $$ | egrep $options "($words)" --color=auto
    echo " ## Starting Watch ($seconds seconds) ##"
    while [ 1 ]; do
    echo -e "\n ## $(date)\n";
    ps axw -o ppid,pid,args,%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;
  4. eddy85br revised this gist Aug 7, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions psgrep.sh
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,7 @@

    options=''
    words=''

    if [ "$#" -eq 0 ]; then
    echo "Must inform at least 1 argument" && exit 1;
    fi
  5. eddy85br revised this gist Aug 7, 2015. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion psgrep.sh
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,10 @@

    options=''
    words=''

    if [ "$#" -eq 0 ]; then
    echo "Must inform at least 1 argument" && exit 1;
    fi

    for arg in "$@"
    do
    if [[ $arg =~ ^- ]]; then
  6. eddy85br revised this gist Apr 17, 2015. 1 changed file with 0 additions and 27 deletions.
    27 changes: 0 additions & 27 deletions swapgrep.sh
    Original file line number Diff line number Diff line change
    @@ -1,27 +0,0 @@
    #!/bin/bash
    # Get current swap usage for all running processes
    # Erik Ljungstrom 27/05/2011
    #
    ## Adapted by Jhonatan Piffer Siqueira and Eduardo Lemons Francisco (eddy85br).

    OVERALL=0
    PROGLIST=$(ps axw -o pid,args --no-headers)

    while read PID ARGS; do
    SUM=0
    if [ -f "/proc/$PID/smaps" ]; then
    for SWAP in $(fgrep 'Swap' /proc/$PID/smaps 2>/dev/null | awk '{ print $2 }') ; do
    let SUM=$SUM+$SWAP
    done
    fi
    if [[ $SUM > 0 ]]; then
    printf "PID: %-6s | Swap used: %-6s KB => %s\n" $PID $SUM "$ARGS"
    else
    printf "Not using Swap, PID: %-6s => %s\n" $PID "$ARGS" 1>/dev/stderr
    fi
    let OVERALL=$OVERALL+$SUM

    done <<<"$PROGLIST"

    echo "Overall swap used: $OVERALL"
    exit 0;
  7. eddy85br created this gist Apr 17, 2015.
    32 changes: 32 additions & 0 deletions psgrep.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    #!/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;
    27 changes: 27 additions & 0 deletions swapgrep.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    #!/bin/bash
    # Get current swap usage for all running processes
    # Erik Ljungstrom 27/05/2011
    #
    ## Adapted by Jhonatan Piffer Siqueira and Eduardo Lemons Francisco (eddy85br).

    OVERALL=0
    PROGLIST=$(ps axw -o pid,args --no-headers)

    while read PID ARGS; do
    SUM=0
    if [ -f "/proc/$PID/smaps" ]; then
    for SWAP in $(fgrep 'Swap' /proc/$PID/smaps 2>/dev/null | awk '{ print $2 }') ; do
    let SUM=$SUM+$SWAP
    done
    fi
    if [[ $SUM > 0 ]]; then
    printf "PID: %-6s | Swap used: %-6s KB => %s\n" $PID $SUM "$ARGS"
    else
    printf "Not using Swap, PID: %-6s => %s\n" $PID "$ARGS" 1>/dev/stderr
    fi
    let OVERALL=$OVERALL+$SUM

    done <<<"$PROGLIST"

    echo "Overall swap used: $OVERALL"
    exit 0;