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) |
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.
|
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.
|
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? |
|
ps -ef | grep <process> | grep -v grep| awk '{ print $2 }' | tr '\n' ','
|
Yes, tr would do it too. Watch out for the 20 item maximum that top has.
|
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 |
Tink, he wants to monitor cpu and memory usage for a set of processes over time. Can you get that from ps alone?
|
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 |
Yes. Nice. Thanks for introducing me to the output format of ps! Never been there before....
|
Quote:
OP, not at your post. ;} |
output pid's from ps to a comma delimited file
pgrep has a delimiter switch, so
Code:
top p $(pgrep -d, tty) Code:
top p $(pgrep bash),$(pgrep -d, tty) |
All times are GMT -5. The time now is 11:03 AM. |