Scripting: formatting ps output w/Perl?
Hello :)
I'm writing a script that gives me some pertinent info about my servers, and the last little piece I need to figure out is the process list. There are going to be non-technical people looking at the output of the script and I'm trying to make it as simple as possible. Right now, if I do ps -ef | grep <process> | grep -v grep It obviously shows me the process, but the problem is that there is 4 lines of info in front of the process. ie root PID date time java -1024 -cp oh,my,god,there,is,so,much,data,it,carries,on,for,4,lines,I,want,to,shoot,myself,trying,to,read,it,f inally,in,the,end,there,is,a,space /here/is/the/process I'd like the output to be something like this: PID: #### Process: /here/is/the/process I've thought about how I could take the output and use awk or sed but I know there is probably an easier way to do it with perl. Can anyone give me a hint on the direction I should be looking? Thanks! Damarr |
How about:
Code:
ps -eo pid,cmd |
Thats close :) but there is still a lot of info I want to cut out from the "cmd"
|
Well have a look through the man pages as I only had a quick glance.
|
I am, I am....however the command itself is very large. I'm trying now to find a way to show only the last part of the command :)
|
Quote:
For example: Code:
grail 2418 2106 0 19:18 ? 00:00:00 python /usr/share/system-config-printer/applet.py |
when I type the command you gave me above this is what I get:
12345 java -Xmx1024m -cp /directory/file.jar:/anotherdirectory/file.jar:/anotherdirectory/file.jar:/anotherdirectory/file.jar: /anotherdirectory/file.jar:/anotherdirectory/file.jar:/anotherdirectory/file.jar:/anotherdirectory/file.jar:/anotherdirectory/file.jar: /anotherdirectory/file.jar:/anotherdirectory/file.jar:/anotherdirectory/file.jar:/anotherdirectory/file.jar:/anotherdirectory/file.jar: /anotherdirectory/file.jar:/anotherdirectory/file.jar:/anotherdirectory/file.jar:/anotherdirectory/file.jar:/anotherdirectory/file.jar: /anotherdirectory/file.jar:/anotherdirectory/file.jar /this/is/the/part/I/want.properties So, all I want is the last little bit where there is a space between the last mentioned .jar and the file. |
In that case I would recommend awk:
Code:
ps -eo pid,cmd | awk '{print $1,$NF}' |
Thanks a ton :) that last bit was what I needed.
Code:
ps -eo pid,cmd | grep <process> | grep -v grep | awk '{print $1,$NF}' |
How about we just use one application to do the work :)
Code:
ps -eo pid,cmd | awk '/<process>/ && !/awk/{print $1,$NF}' |
what would be the equivalent of "|grep -v /dev/null" for awk? There are a couple of processes coming up as
PID /dev/null |
Code:
&& !"/dev/null" |
Thanks!
|
All times are GMT -5. The time now is 05:06 PM. |