| | |
cant get shell script to work
![]() |
•
•
Join Date: Dec 2007
Posts: 17
Reputation:
Solved Threads: 0
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:
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
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)
#!/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
Last edited by cscgal; Sep 8th, 2008 at 3:38 pm. Reason: Added code tags
First of all, you should use
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).
Shell Scripting Syntax (Toggle Plain Text)
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
----------------------------------------------
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
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. ;-)
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
----------------------------------------------
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
Try this!
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
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.
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:
I played with different numbers in the if statement, and it seems to do the job.
Hope this helps!
-G
Shell Scripting Syntax (Toggle Plain Text)
$ 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
•
•
Join Date: Apr 2006
Posts: 148
Reputation:
Solved Threads: 40
•
•
•
•
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)
$ 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
Shell Scripting Syntax (Toggle Plain Text)
ps -C bash -o size= | awk '{sum+=$1}END{ print (sum>65535 ? "exceeded":"not exceeded") }'
![]() |
Similar Threads
- Logging into a page automatically using shell script. (Shell Scripting)
- Want to write script to back-up up Linux hdb drive to hdc drice (*nix Hardware Configuration)
- exiting the shell within a shell script (Shell Scripting)
- shell script as a daemon (Shell Scripting)
- help with shell script padding files with spaces (Shell Scripting)
- Linux Shell Script needs help writing program (Shell Scripting)
- Problem with variables in Windows shell script (Windows NT / 2000 / XP)
- Using find in a bash shell script (Shell Scripting)
Other Threads in the Shell Scripting Forum
- Previous Thread: File Name with Space
- Next Thread: batch file to delete files based on the criteria
| Thread Tools | Search this Thread |






