Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
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.
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.
for line in `cat /tmp/pids`
do
awk ' {if(index(FILENAME,"domain")!=0){split(FILENAME,a,"/");app=a["6"];printf "%25s %5d %10d\n", app" ", $1" ", $1 }};
{if(index(FILENAME,"nodeagent")!=0){split(FILENAME,a,"/");app=a["7"];printf "%25s %5d %10d\n", app" ", $1" ", $1}} ' $line
done
What this script performs currently is display the jvm name and pid. I want to add the jvm mem allocated. btw, the command works outside of the script when I input a valid pid.
To start with, a single awk or sed would be much better than grep+tail.
But then you're piping the output into echo, which is a command that doesn't read from stdin.
Finally, there's a pointless call to expr, when the shell has perfectly good integer math built right into it. (And $(..) is highly recommended over `..`, too.)
Oh, and by the way:
Code:
for line in `cat /tmp/pids`
Don't Read Lines With For! Always use a while+read loop when working with the output of files and commands.
I'll try the system command and see what the result is [thank you tronayne].
I would try the awk/sed line if I knew what it would look like. David, I appreciate what you are saying but if you are going to correct someone who is asking/learning this, might I suggest providing the proper syntax for what you are explaining. I created the for loop many years ago and have had not an issue with it's performance. I would be willing to re-write this in the future with the while loop as I understand why. What I need is the
at the moment working with the existing script due to time constraints. If you have the knowledge of writing this in awk, please provide so I can test with my existing awk statement and implement this. I appreciate your input and knowledge sharing.
regards.
The addition of the format string ("%-45s %d\n") makes the output "look nicer;" not necessary but a little easier to read. Plus you do the arithmetic in the printf.
I suspect you're running this in a loop of some sort with a sleep? Sorta like
Code:
while true
do
ps -AH v | tail -1 | awk '{ printf ("%-45s %d\n", $10, $7/1024) }'
sleep 10
done
and you let it run for a while and watch it then abort with Ctrl-C?
I would try the awk/sed line if I knew what it would look like. David, I appreciate what you are saying but if you are going to correct someone who is asking/learning this, might I suggest providing the proper syntax for what you are explaining.
I didn't provide a solution because I'm not sure what you are trying to do exactly. The error of trying to pipe a command into echo is particularly confusing.
Quote:
I created the for loop many years ago and have had not an issue with it's performance. I would be willing to re-write this in the future with the while loop as I understand why.
Using a for loop like that is not actually a problem as long as the command produces output in individual "words", and doesn't include word-breaking whitespace or globbing patterns that could expand. But it's never a bad idea to get in the habit of doing it correctly, even when unnecessary. Better safe than sorry.
How would I incorporate this in the EXISTING awk script to display in the proper column/line location corresponding to the appropriate JVM???
So far I have:
Code:
#!/bin/bash
### this command will provide the memory the JVM[pid] is currently allocatted
### ps -AH v | grep 29742 | tail -1 | echo `expr $7 / 1024`
clear
sudo find /opt/SUN/SUNAppSrv -name .__com* | grep domain > /tmp/pids
sudo find /opt/SUN/SUNAppSrv -name .__com* | grep nodeagent >> /tmp/pids
printf " Java Name\t\t PID MEM\n------------------------ -------- ------\n"
while read -r line; do
awk ' {if(index(FILENAME,"domain")!=0){split(FILENAME,a,"/");app=a["6"];printf "%25s %5d %10d\n", app" ", $1" ", $1 }};
{if(index(FILENAME,"nodeagent")!=0){split(FILENAME,a,"/");app=a["7"];printf "%25s %5d %10d\n", app" ", $1" ", $1 }} ' $line
done < /tmp/pids
what I am not sure of is how/where to put the command to obtain the memory allocated to the JVM. Can I even perform this inside the awk statement or does the value need to be obtained outside the awk and how is that done then printed in the appropriate line/column within the awk statement.
thank you for your input/advice. I'm sure it can be written more efficiently/effective but it works in my pinch at the moment. If you feel compelled to improve on it, then by all means please advance this.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.