How to create a bash script with the given full command of pmap?

This is using SuSE 11 with SP4. Recently, I came across an online article discussed about how to use pmap to display the actual mapping, memory in active/cached and so on.
Please see the full command below,

 for i in `ps -eaf | grep “any application process” | grep -v grep | awk '{print $2}'`; do echo -n "PID $i actual memory usage is :" >> totaluse.txt; pmap -d $i | grep -i "writeable/private: " >> totaluse.txt; done

I was ended up with "nothing" when copy and paste the whole command to the terminal. Can I create a bash script? if so, please enlighten how, thanks.
MichaelBalackSenior System EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Most likely you're finding no PIDs or the PIDs have already died at time pmap is called.

Try this command, which will work on any running machine.

pmap -d 1

Open in new window

Now break your script into sections + print debug info...

So... get your PID list + print out all the PIDs + command/args.

Then cycle through your PID list printing every pmap command before running the command.

Basic, incremental debugging, will provide enough data to fix your code.

Tip: Also print out information messages, like PID count + a warning each time pmap runs on a PID + returns no result. Be sure to add additional code to test if PID is still alive, when you get no return data from pmap.
did you replace "any application process" with the name of a runniing process ?
pgrep -af SOMETHING_YOU_NEED_TO_REPLACE | wile read pid rest
 echo mapping for $pid $rest
 LC_ALL=en pmap -d $i | grep -i "writeable/private: "

if you want to redirect the output, add ">> /path/to/output" at the end

the same will run as a one liner with additionnal ; where needed

if you need to run this for a huge set of processes, you might consider outputting the mapped info and processs name in a single echo and fork or possibly use xargs

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Better to just pick a solution and allow this question/comments to persist, as this may help someone down the road.
MichaelBalackSenior System EngineerAuthor Commented:
Thanks for both experts. Your suggestions are helpful.
I'd rather be actually helpful. If you want, i have a script that can gather the real memory usage of each process systemwide based on pmap output.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.