LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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



Reply
 
Search this Thread
Old 04-07-2010, 05:20 AM   #1
machielr
LQ Newbie
 
Registered: Feb 2010
Posts: 27

Rep: Reputation: 0
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)
 
Old 04-07-2010, 05:43 AM   #2
scobiej
Member
 
Registered: Aug 2003
Location: London
Distribution: Redhat, SuSE, Ubuntu
Posts: 75

Rep: Reputation: 15
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.
 
Old 04-07-2010, 05:48 AM   #3
bakdong
Member
 
Registered: Apr 2009
Posts: 214

Rep: Reputation: 44
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.
 
Old 04-07-2010, 07:15 AM   #4
machielr
LQ Newbie
 
Registered: Feb 2010
Posts: 27

Original Poster
Rep: Reputation: 0
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?
 
Old 04-07-2010, 08:22 AM   #5
bakdong
Member
 
Registered: Apr 2009
Posts: 214

Rep: Reputation: 44
Try this

http://sed.sourceforge.net/sed1line.txt

sed 's/$/,/' input.filename > output.filename
 
Old 04-07-2010, 08:35 AM   #6
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,652

Rep: Reputation: 536Reputation: 536Reputation: 536Reputation: 536Reputation: 536Reputation: 536
ps -ef | grep <process> | grep -v grep| awk '{ print $2 }' | tr '\n' ','
 
Old 04-08-2010, 12:45 AM   #7
bakdong
Member
 
Registered: Apr 2009
Posts: 214

Rep: Reputation: 44
Yes, tr would do it too. Watch out for the 20 item maximum that top has.
 
Old 04-08-2010, 01:48 AM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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
 
Old 04-08-2010, 02:31 AM   #9
bakdong
Member
 
Registered: Apr 2009
Posts: 214

Rep: Reputation: 44
Tink, he wants to monitor cpu and memory usage for a set of processes over time. Can you get that from ps alone?
 
Old 04-08-2010, 02:36 AM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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
 
Old 04-08-2010, 02:49 AM   #11
bakdong
Member
 
Registered: Apr 2009
Posts: 214

Rep: Reputation: 44
Yes. Nice. Thanks for introducing me to the output format of ps! Never been there before....
 
Old 04-08-2010, 04:33 AM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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. ;}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using sed to remove line in a comma-delimited file seefor Programming 4 03-10-2009 04:35 PM
column re-alignment - space delimited to comma delimited hattori.hanzo Linux - Newbie 9 03-05-2009 01:54 AM
separating a comma delimited line mrobertson Programming 7 07-27-2005 02:56 PM
Comma-Delimited Website Filenames Apocalypse General 1 11-09-2003 10:05 AM
comma delimited file cdragon Programming 5 06-21-2002 08:55 PM


All times are GMT -5. The time now is 12:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration