0

I searched a lot on the internet but somehow nothing works. I got an application called myApp and I want to display the memory usage of this app at discrete points of time. I need sth. like this:

#!/bin/bash
for((i=0;i<=20;++i))
do
  ... check memory usage here
  sleep 3
done

I cannot use pidof since the command is not found. Till now, I got this:

#!/bin/bash

PROCESSNAME=myApp
MYPID=`pgrep -f $PROCESSNAME`

echo "=======";
echo PID:$MYPID
echo "--------"
Rss=`echo 0 $(cat /proc/$MYPID/smaps  | grep Rss | awk '{print $2}' | sed 's#^#+#') | bc;`
Shared=`echo 0 $(cat /proc/$MYPID/smaps  | grep Shared | awk '{print $2}' | sed 's#^#+#') | bc;`
Private=`echo 0 $(cat /proc/$MYPID/smaps  | grep Private | awk '{print $2}' | sed 's#^#+#') | bc;`
Swap=`echo 0 $(cat /proc/$MYPID/smaps  | grep Swap | awk '{print $2}' | sed 's#^#+#') | bc;`
Pss=`echo 0 $(cat /proc/$MYPID/smaps  | grep Pss | awk '{print $2}' | sed 's#^#+#') | bc;`

Mem=`echo "$Rss + $Shared + $Private + $Swap + $Pss"|bc -l`

echo "Rss     " $Rss
echo "Shared  " $Shared
echo "Private " $Private
echo "Swap    " $Swap
echo "Pss     " $Pss
echo "=================";
echo "Mem     " $Mem
echo "=================";

I call this myApp from NetBeans (it's a C++ program). Unfortunately, pgrep -f myApp returns two PIDs so nothing works in my script. Actually, I have nearly no knowledge about bash-scripts and thus, no idea what to do now.

Another question: Looking at "System Monitor" of Linux the "Memory"-column gives me other values than this script here. Can you tell me why? Which of the Types (Rss, Shared,...) equals this "Memory"-column of "System Monitor"?

Kapa11
  • 311
  • 2
  • 18
  • Try with the `-x` option to return only one exactly matching id, as `pgrep -fx myApp` – Inian Dec 21 '16 at 11:07
  • Using `-fx` returns nothing, using `pgrep -x myApp` only returns one ID. Thanks. Can you tell me which of these values represents the memory `myApp` actually needs? And in which unit is it measured? – Kapa11 Dec 21 '16 at 11:10
  • Try running `ps -p ` to see if returns your app name properly as `myapp` – Inian Dec 21 '16 at 11:16
  • @Inian For the 2nd ID (which is also taken by `pgrep -x`) the name is displayed properly. For the first PID it says CMD: `pty`. – Kapa11 Dec 21 '16 at 11:19
  • Does that mean it is returning correctly or wrong? – Inian Dec 21 '16 at 11:21
  • Why would you need to add zero to each result, and why don't you do that in Awk if you have to? `swap=$(awk '/Swap/ { print 0 + $2 }' /proc/$MYPID/smaps)` – tripleee Dec 21 '16 at 11:56

0 Answers0