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 ...
- 06-29-2011 #1Just Joined!
- Join Date
- Jun 2011
- Location
- Bangalore, IN
- Posts
- 8
regx in shell script
#---End---#Code:#!/bin/bash cmd=`ps -ef | grep web | grep -v grep | awk '{print $2}'` top -b -p $cmd -d 60 >> $1
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
- 06-29-2011 #2Just Joined!
- Join Date
- Jun 2011
- Location
- Bangalore, IN
- Posts
- 8
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#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
- 06-29-2011 #3Just Joined!
- Join Date
- Apr 2009
- Posts
- 3
That's a pretty neat idea.
How about a one-liner:
(Finds instances of 'chrome' rather than 'web'.)Code:echo `ps -ef | grep chrom[e] | awk '{print $2}'` | sed 's/ /,/g' | awk '{print "top -b -p "$1}' | sh
I just added it as a function in my .bashrc:
Then you can just call it like: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 }
Code:top-for web
- 06-30-2011 #4Just 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.
- 06-30-2011 #5Just Joined!
- Join Date
- Jun 2011
- Location
- Bangalore, IN
- Posts
- 8
@ddoxey : really nice idea
tried and it worked
- 06-30-2011 #6Just 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.
- 07-01-2011 #7Just 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


Reply With Quote