cant get shell script to work

Reply

Join Date: Dec 2007
Posts: 17
Reputation: yair7190 is an unknown quantity at this point 
Solved Threads: 0
yair7190 yair7190 is offline Offline
Newbie Poster

cant get shell script to work

 
0
  #1
Sep 8th, 2008
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:
  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,357
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: cant get shell script to work

 
0
  #2
Sep 8th, 2008
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).
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 17
Reputation: yair7190 is an unknown quantity at this point 
Solved Threads: 0
yair7190 yair7190 is offline Offline
Newbie Poster

Re: cant get shell script to work

 
0
  #3
Sep 8th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,357
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: cant get shell script to work

 
0
  #4
Sep 8th, 2008
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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 61
Reputation: Gromit is an unknown quantity at this point 
Solved Threads: 7
Gromit's Avatar
Gromit Gromit is offline Offline
Junior Poster in Training

Re: cant get shell script to work

 
0
  #5
Sep 8th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 61
Reputation: Gromit is an unknown quantity at this point 
Solved Threads: 7
Gromit's Avatar
Gromit Gromit is offline Offline
Junior Poster in Training

Re: cant get shell script to work

 
0
  #6
Sep 8th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 17
Reputation: yair7190 is an unknown quantity at this point 
Solved Threads: 0
yair7190 yair7190 is offline Offline
Newbie Poster

Re: cant get shell script to work

 
0
  #7
Sep 8th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: cant get shell script to work

 
0
  #8
Sep 15th, 2008
Originally Posted by Gromit View Post
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") }'
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC