Find the answer to your Linux question:
Results 1 to 7 of 7
Code: #!/bin/bash cmd=`ps -ef | grep web | grep -v grep | awk '{print $2}'` top -b -p $cmd -d 60 >> $1 #---End---# In the above script first i ...
  1. #1
    Just Joined!
    Join Date
    Jun 2011
    Location
    Bangalore, IN
    Posts
    8

    regx in shell script

    Code:
    #!/bin/bash
    cmd=`ps -ef | grep web | grep -v grep | awk '{print $2}'`
    top -b -p $cmd -d 60 >> $1
    #---End---#
    In the above script first i will grep the process ids of all the processes running with the name 'web'.
    and after that i will start a top process to capture the mem and other resources utilized by the process 'web'
    -------------------
    in my case cmd will return output as:
    123
    345
    456
    but as you can see in my top command , we need to pass process ids as:
    top -b -p <pid1,pid2,pid3>
    which means i need to pass something like: 123,345,456
    so can someone please help me to write a regx to get the pids in above from.

    Thanks
    tanuj

  2. #2
    Just Joined!
    Join Date
    Jun 2011
    Location
    Bangalore, IN
    Posts
    8

    Thumbs up

    Got the solution, i was trying the same thing with before but was trying to replace a whitesapce with comma (,). after some attempt i tried replacing non-numeric char with comma (,) and that worked..

    bash-3.00#
    Code:
    abc=`ps -ef | grep web | grep -v grep | awk '{print $2}'`
    bash-3.00#
    Code:
    echo $abc
    4954 4966 5023
    bash-3.00#
    Code:
    echo $abc | sed s/[^0-9]/,/g
    4954,4966,5023
    bash-3.00#

  3. #3
    Just Joined!
    Join Date
    Apr 2009
    Posts
    3
    That's a pretty neat idea.

    How about a one-liner:
    Code:
    echo `ps -ef | grep chrom[e] | awk '{print $2}'` | sed 's/ /,/g' | awk '{print "top -b -p "$1}' | sh
    (Finds instances of 'chrome' rather than 'web'.)

    I just added it as a function in my .bashrc:
    Code:
    function top-for() {
    
        echo `ps -ef | grep $1 | grep -v grep | awk '{print $2}'` | sed 's/ /,/g' | awk '{print "top -b -p "$1}' | sh
    }
    Then you can just call it like:
    Code:
    top-for web

  4. #4
    Just Joined!
    Join Date
    Apr 2010
    Posts
    69
    You know, if the command is available, you could use pgrep and get rid of that "ps -ef" and "grep -v grep" stuff.

  5. #5
    Just Joined!
    Join Date
    Jun 2011
    Location
    Bangalore, IN
    Posts
    8
    @ddoxey : really nice idea tried and it worked

  6. #6
    Just Joined!
    Join Date
    Nov 2007
    Posts
    2
    Hi,
    pgrep is really a good option. You can also use "pgrep -d ," to return a "," separated list.

  7. #7
    Just Joined!
    Join Date
    Jun 2011
    Location
    Bangalore, IN
    Posts
    8
    @achaplot: cool this also worked. Thanks Man!

    bash-3.00# ps -ef | pgrep -d "," web
    230,231

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...