hi everyone,

im trying to write a shell script that calculates memory usage of several processes (i basically run bash several times). here's the script:

#!/usr/bin/bash
memUsage=`ps -ef | grep bash| awk '{sum+= $2}'`
echo $memUsage


if  $memUsage>130000000000000 ; then
        echo "mem usage exceeded"
     else
        echo "mem usage normall"
fi
exit 0

first of all, echo doesn't print the value of memUsage.
second of all, no matter what i enter after ">" (1, 130 or 13000000000), it always echos "mem usage exceeded" and exits.

can someone please tall what im doing wrong before i'll lose it?
thank you

First of all, you should use

ps -ef | grep bash | grep -v grep

otherwise you sometimes get the process for "grep bash" as well.

Also, "$2" in a ps -ef command is the ProcessId, not the memory usage. The memory usage is not printed at all in ps -ef. You need to use the o option combined with "SZ" to get the memory size (but be aware that that is the "virtual" memory size, not necessarily the amount of memory currently in use by that process).

you're wright, i ignored that since my goal was to make the script work (regardless if its a PID, MEM, etc ).

still, i dont get why echo doesn't print the value of memUsage and why the if statement doesn't work.
any help appreciated :)

because you're awk statement doesn't produce anything. It sums, but does not print.

Edit: And I don't really know if it sums correctly. It's been ages since I've done anything with awk, so I'd have to look it up in order to be able to give you the correct syntax, and, truth be told, I don't really feel like it, right now. ;-)

Try this! ps -C bash -o pid=,size | awk '{SUM += $2} END {print SUM}' The problem is that your awk statement doesn't have a "print" command. You have to tell it what to print.

I also took some liberties in my example, and used some built in functions of PS rather than piping it through grep ;) The output of that ps statement is the pid and memory usage (in KB, so the final answer is also in KB)

Hope this helps!
-G

I poked at your original script and made some syntax changes to your "if" statement. I think it does what you want now. I think you have to use brackets when doing a comparison like this:

$ cat daniweb.sh 
#!/usr/bin/bash
memUsage=$(ps -C bash -o pid=,size|awk '{SUM += $2} END {print SUM}')

echo "total usage is: $memUsage"

if [ "$memUsage" -gt 65536 ]; then
echo "mem usage exceeded"
else
echo "mem usage normal"
fi
exit 0

I played with different numbers in the if statement, and it seems to do the job.

Hope this helps!
-G

thanks a lot for replaying.

thank you Gromit, your last post made me realize my mistakes.

until next time...
hope i wont be back here to soon :)

I poked at your original script and made some syntax changes to your "if" statement. I think it does what you want now. I think you have to use brackets when doing a comparison like this:

$ cat daniweb.sh 
#!/usr/bin/bash
memUsage=$(ps -C bash -o pid=,size|awk '{SUM += $2} END {print SUM}')

echo "total usage is: $memUsage"

if [ "$memUsage" -gt 65536 ]; then
echo "mem usage exceeded"
else
echo "mem usage normal"
fi
exit 0

I played with different numbers in the if statement, and it seems to do the job.

Hope this helps!
-G

you can do everything in awk

ps -C bash -o size= | awk '{sum+=$1}END{ print (sum>65535 ? "exceeded":"not exceeded") }'
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.