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

MichaelBalack used Ask the Experts™
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.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
David FavorFractional CTO
Distinguished Expert 2018

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
11/26 Forrester Webinar: Savings for Enterprise

How can your organization benefit from savings just by replacing your legacy backup solutions with Acronis' #CyberProtection? Join Forrester's Joe Branca and Ryan Davis from Acronis live as they explain how you can too.

David FavorFractional CTO
Distinguished Expert 2018

Better to just pick a solution and allow this question/comments to persist, as this may help someone down the road.
MichaelBalackSenior System Engineer


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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial