Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
04-07-2010, 04:20 AM
|
#1
|
LQ Newbie
Registered: Feb 2010
Posts: 27
Rep:
|
output pid's from ps to a comma delimited file
Hi All
I hope that someone can perhaps assist me here.
As part of an audit I am busy doing I need to monitor pecific processes over a time frame in terms of the amount of memory and cpu usage it utilizes.
I can do this using the top -p <pid> option and using ps to retrieve the pid's.
However, seeing that the pid's might differ and it needs to be run on about 13 different machines, I would like to write a script for this that can be run at set intervals.
My problem that I have is this:
- When running top -p <pid> I can specify a comma seperated list of the processes required to monitor at that specific time.
- I can use ps -ef | grep <process> | grep -v grep| awk '{ print $2 }' to retrive the list of pid's and output this to a file.
However, how can I output these to the file as a comma seperated list without having to manually do this every time?
The reason for this is (an example), lets say I want to monitor the cpu and memory usage of postgresql as well as all its child processes, then I would ps grep for postgres and get the list of pid's for instance.
This list then needs to be passed to top -p as a comma seperated list of pid's
I suspect that awk or sed might have some options available for this but I do not know this well enough.
I would appreciate any assistance here, even if it is a suggestion of better ways to do this. (plese note that due to security restriction I am not able to install any packages and need to use linux native tools)
|
|
|
04-07-2010, 04:43 AM
|
#2
|
Member
Registered: Aug 2003
Location: London
Distribution: Redhat, SuSE, Ubuntu
Posts: 75
Rep:
|
Perl is your friend here ...
Seriously, that is why it is called Practical Extraction and Reporting Language. It was invented by Larry Wall to solve sys admin problems just like this. A bit of googling and you will easily find a solution for it.
|
|
|
04-07-2010, 04:48 AM
|
#3
|
Member
Registered: Apr 2009
Posts: 214
Rep:
|
Or if you don't want to go as far as perl you could just use sed to change the line feeds to commas in the ps output file.
|
|
|
04-07-2010, 06:15 AM
|
#4
|
LQ Newbie
Registered: Feb 2010
Posts: 27
Original Poster
Rep:
|
sed commands?
Hi Bakdong,
Sorry, but I am not familiar with the sed commands, can you perhaps assist with this or maybe point me to a resource to get the info?
|
|
|
04-07-2010, 07:35 AM
|
#6
|
Senior Member
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824
|
ps -ef | grep <process> | grep -v grep| awk '{ print $2 }' | tr '\n' ','
|
|
|
04-07-2010, 11:45 PM
|
#7
|
Member
Registered: Apr 2009
Posts: 214
Rep:
|
Yes, tr would do it too. Watch out for the 20 item maximum that top has.
|
|
|
04-08-2010, 12:48 AM
|
#8
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
I feel you might be overthinking this problem. Just slap
the names of the processes you need to monitor into a text
file, and go:
Code:
ps -e -o pcpu,rss,comm | grep -f processes
Cheers,
Tink
|
|
|
04-08-2010, 01:31 AM
|
#9
|
Member
Registered: Apr 2009
Posts: 214
Rep:
|
Tink, he wants to monitor cpu and memory usage for a set of processes over time. Can you get that from ps alone?
|
|
|
04-08-2010, 01:36 AM
|
#10
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
run it in a loop, still more efficient than invoking 4-5 processes
to feed into top ... it's not like you get statistics from top, either.
Heck, you can (if you don't want to save the output to a file for
accounting) even just run the thing above under watch ;}
Code:
watch -n 1 'ps -e -o pcpu,rss,comm | grep -f processes'
Cheers,
Tink
|
|
|
04-08-2010, 01:49 AM
|
#11
|
Member
Registered: Apr 2009
Posts: 214
Rep:
|
Yes. Nice. Thanks for introducing me to the output format of ps! Never been there before....
|
|
|
04-08-2010, 03:33 AM
|
#12
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Quote:
Yes. Nice. Thanks for introducing me to the output format of ps! Never been there before....
|
My pleasure - btw, the "overthinking comment" was aimed at the
OP, not at your post. ;}
|
|
|
06-26-2016, 06:15 PM
|
#13
|
LQ Newbie
Registered: Jun 2016
Posts: 1
Rep:
|
output pid's from ps to a comma delimited file
pgrep has a delimiter switch, so
Code:
top p $(pgrep -d, tty)
or
Code:
top p $(pgrep bash),$(pgrep -d, tty)
|
|
|
All times are GMT -5. The time now is 12:22 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|