LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-01-2012, 01:56 AM   #1
Vi_rod
Member
 
Registered: Dec 2011
Posts: 42

Rep: Reputation: Disabled
Help with Free memory script


Hi,
I need help with writing a script for checking if free memory drops below a certain level, execute - echo 1 > /proc/sys/vm/drop_caches.
So far, i could come up with this -
#!/bin/bash

set -xv
g=`free -m | awk '{if ($2 > 3400) print $2 }'`
if [$g ge 3400]
then
echo 1 > /proc/sys/vm/drop_caches
fi


The error i get as i execute - >
g=`free -m | awk '{if ($2 > 3400) print $2 }'`
free -m | awk '{if ($2 > 3400) print $2 }'
++ free -m
++ awk '{if ($2 > 3400) print $2 }'
+ g='used
3470
buffers/cache:
4094'
if [$g ge 3400]
then
echo 1 > /proc/sys/vm/drop_caches
fi
+ '[used' 3470 buffers/cache: 4094 ge '3400]'
./test_script.sh: line 5: [used: command not found



Any other way to achieve the same? please advice
 
Old 02-01-2012, 03:09 AM   #2
rodrifra
Member
 
Registered: Mar 2007
Location: Spain
Distribution: Debian
Posts: 201

Rep: Reputation: 36
You have to avoid text, so

Code:
free -m | awk '{if ($2 > 3400 && NR> 1) print $2 }'
Should work if you have the results in the fields you expect them to, but in my system the next line does not follow the rule

Code:
-/+ buffers/cache:        500       2520
so you might need to avoid that line with

Code:
free -mo | awk '{if ($2 > 3400 && NR> 1) print $2 }'
 
1 members found this post helpful.
Old 02-01-2012, 03:20 AM   #3
westy500
LQ Newbie
 
Registered: Dec 2010
Location: UK
Distribution: Debian 7.5 (Wheezy)
Posts: 5

Rep: Reputation: 1
Looks like your awk statement is not cutting the integer value properly and there is a missing '-' in the if statement.

Have a look at the attached.
Attached Files
File Type: txt memWarner.txt (385 Bytes, 15 views)
 
Old 02-01-2012, 05:10 AM   #4
Vi_rod
Member
 
Registered: Dec 2011
Posts: 42

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by westy500 View Post
Looks like your awk statement is not cutting the integer value properly and there is a missing '-' in the if statement.

Have a look at the attached.
Cool!! Neat work, Thanks

I did not understand just one line -
| awk -F"." '{print $1}'`

I presume it removes decimal value .. but did not understand the logic

Last edited by Vi_rod; 02-01-2012 at 05:55 AM.
 
Old 02-01-2012, 06:45 AM   #5
rodrifra
Member
 
Registered: Mar 2007
Location: Spain
Distribution: Debian
Posts: 201

Rep: Reputation: 36
-F replaces the default field separator for the one you specify. So the new field separator will be '.' in that awk statement.
 
Old 02-01-2012, 06:27 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,295

Rep: Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358
@OP: why would you even do this? Linux has a very good mem scheduler; dropping the cache will only slow it down until it re-caches...
 
Old 02-02-2012, 06:54 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

Your test lines are completely broken:

Code:
if [$g ge 3400]

1) "[" is a command, and the shell must parse the arguments following the command by reading whitespace. You have no whitespace, so the command breaks.

2) QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a variable expansion unless you explicitly want the resulting string to be word-broken by the shell. This is particularly important inside the "[" test brackets, as the wrong number of arguments will break the comparison.

3) "ge" is not a valid comparator. You need to use "-ge" for numerical comparisons.

However, since you're using bash, you really should avoid using "[" altogether, and use the newer "[[..]]" for string tests and "((..))" for arithmetic tests.

http://mywiki.wooledge.org/BashFAQ/031
http://mywiki.wooledge.org/ArithmeticExpression


So I recommend changing the above to:
Code:
if (( g >= 3400 ))
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to free memory immediately after calling free in C programming golden_boy615 Programming 2 12-03-2011 07:09 AM
looking for free database that has low memory usage and innodb and memory like engin golden_boy615 Linux - Software 8 05-08-2011 07:05 AM
Application Virtual address space memory allocation - memory does not get free chamara82 Linux - General 4 01-01-2011 08:19 PM
[SOLVED] Free-ing memory problem. C/C++, free() sevs Programming 6 03-13-2010 12:31 PM
[SOLVED] Memory leak: How risky not to free allocated memory. kaz2100 Linux - General 1 12-24-2008 12:00 AM


All times are GMT -5. The time now is 11:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration