LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   output pid's from ps to a comma delimited file (https://www.linuxquestions.org/questions/linux-general-1/output-pids-from-ps-to-a-comma-delimited-file-800534/)

machielr 04-07-2010 04:20 AM

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)

scobiej 04-07-2010 04:43 AM

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.

bakdong 04-07-2010 04:48 AM

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.

machielr 04-07-2010 06:15 AM

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?

bakdong 04-07-2010 07:22 AM

Try this

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

sed 's/$/,/' input.filename > output.filename

AlucardZero 04-07-2010 07:35 AM

ps -ef | grep <process> | grep -v grep| awk '{ print $2 }' | tr '\n' ','

bakdong 04-07-2010 11:45 PM

Yes, tr would do it too. Watch out for the 20 item maximum that top has.

Tinkster 04-08-2010 12:48 AM

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

bakdong 04-08-2010 01:31 AM

Tink, he wants to monitor cpu and memory usage for a set of processes over time. Can you get that from ps alone?

Tinkster 04-08-2010 01:36 AM

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

bakdong 04-08-2010 01:49 AM

Yes. Nice. Thanks for introducing me to the output format of ps! Never been there before....

Tinkster 04-08-2010 03:33 AM

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. ;}

Stu0 06-26-2016 06:15 PM

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 11:03 AM.