943,729 Members | Top Members by Rank

Ad:
Sep 8th, 2008
0

cant get shell script to work

Expand Post »
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:
bash Syntax (Toggle Plain Text)
  1. #!/usr/bin/bash
  2. memUsage=`ps -ef | grep bash| awk '{sum+= $2}'`
  3. echo $memUsage
  4.  
  5.  
  6. if $memUsage>130000000000000 ; then
  7. echo "mem usage exceeded"
  8. else
  9. echo "mem usage normall"
  10. fi
  11. 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
Last edited by cscgal; Sep 8th, 2008 at 3:38 pm. Reason: Added code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yair7190 is offline Offline
17 posts
since Dec 2007
Sep 8th, 2008
0

Re: cant get shell script to work

First of all, you should use
Shell Scripting Syntax (Toggle Plain Text)
  1. 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).
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Sep 8th, 2008
0

Re: cant get shell script to work

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yair7190 is offline Offline
17 posts
since Dec 2007
Sep 8th, 2008
0

Re: cant get shell script to work

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. ;-)
Last edited by masijade; Sep 8th, 2008 at 8:56 am.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Sep 8th, 2008
0

Re: cant get shell script to work

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
Last edited by Gromit; Sep 8th, 2008 at 1:58 pm.
Reputation Points: 46
Solved Threads: 28
Junior Poster
Gromit is offline Offline
183 posts
since Sep 2008
Sep 8th, 2008
0

Re: cant get shell script to work

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:

Shell Scripting Syntax (Toggle Plain Text)
  1. $ cat daniweb.sh
  2. #!/usr/bin/bash
  3. memUsage=$(ps -C bash -o pid=,size|awk '{SUM += $2} END {print SUM}')
  4.  
  5. echo "total usage is: $memUsage"
  6.  
  7. if [ "$memUsage" -gt 65536 ]; then
  8. echo "mem usage exceeded"
  9. else
  10. echo "mem usage normal"
  11. fi
  12. exit 0

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

Hope this helps!
-G
Reputation Points: 46
Solved Threads: 28
Junior Poster
Gromit is offline Offline
183 posts
since Sep 2008
Sep 8th, 2008
0

Re: cant get shell script to work

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yair7190 is offline Offline
17 posts
since Dec 2007
Sep 15th, 2008
0

Re: cant get shell script to work

Click to Expand / Collapse  Quote originally posted by Gromit ...
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:

Shell Scripting Syntax (Toggle Plain Text)
  1. $ cat daniweb.sh
  2. #!/usr/bin/bash
  3. memUsage=$(ps -C bash -o pid=,size|awk '{SUM += $2} END {print SUM}')
  4.  
  5. echo "total usage is: $memUsage"
  6.  
  7. if [ "$memUsage" -gt 65536 ]; then
  8. echo "mem usage exceeded"
  9. else
  10. echo "mem usage normal"
  11. fi
  12. 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
Shell Scripting Syntax (Toggle Plain Text)
  1. ps -C bash -o size= | awk '{sum+=$1}END{ print (sum>65535 ? "exceeded":"not exceeded") }'
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: File Name with Space
Next Thread in Shell Scripting Forum Timeline: batch file to delete files based on the criteria





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC