LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-22-2007, 10:02 AM   #1
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Rep: Reputation: 17
Unhappy Is it possible? A script to check free RAM and reboot if it is less than a limit


Can i write a script which monitor RAM free space by 'free -m' command, and if the free space is less than certain limit, it should reboot the system.
I tried free -m the result was
total used free shared buffers cached
Mem: 249 60 189 0 15 26
-/+ buffers/cache: 18 231
Swap: 509 0 509

we should cut the column 'free' as an integer value. Is it possible? Am i moving in a right angle? Please suggest me....
 
Old 11-22-2007, 10:11 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 67
Yes of course it is possible, but it is almost certainly not the right approach. Linux aggressively assigns RAM to the IO cache, so there is rarely very much RAM free, even right after reboot.

If you have some process which is hogging all your memory, killing that process (or fixing the program not to hog resources) a better solution.

Last edited by matthewg42; 11-22-2007 at 10:38 PM. Reason: typo : hod -> hog
 
Old 11-22-2007, 10:13 AM   #3
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Original Poster
Rep: Reputation: 17
Unhappy

Or is there any tools available for it?
 
Old 11-22-2007, 10:38 AM   #4
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Original Poster
Rep: Reputation: 17
Angry

I used 'free -mo' and i inserted the result in a file. But i cant cut the particular field named as free

total used free shared buffers cached
Mem: 249 60 189 0 15 26
Swap: 509 0 509


I think the delimiter is not both tab and :

I used

cut -f4 filename

this is not working.....
 
Old 11-22-2007, 10:51 AM   #5
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Original Poster
Rep: Reputation: 17
Angry

When i used cat command, the answer was
total used free shared buffers cached
Mem: 249 60 189 0 15 26
Swap: 509 0 509

when i use 'cut -f1 temp.txt'
Entire file was printing

When i use 'cut -d: -f1 temp.txt'
total used free shared buffers cached
Mem
Swap

when i use 'cut -d: -f2 temp.txt
total used free shared buffers cached
249 60 189 0 15 26
509 0 509
likewise i cant get what i want. I think i have to change my direction. Is there any other ......?
 
Old 11-22-2007, 11:05 AM   #6
anon237
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Free uses a space as delimiter, not a tab.

If you want the bold part only:
Code:
total used free shared buffers cached
Mem: 249 60 189 0 15 26
Swap: 509 0 509
you could try this: free -mo | awk '/Mem/ { print $3 }'

This awk snippet prints the third field (print $3) only if Mem (/Mem/) is present in that line.

Hope this helps.
 
Old 11-22-2007, 11:40 AM   #7
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Original Poster
Rep: Reputation: 17
Unhappy

Ya its working. Thanks. The result is a string or an integer? How to check that...
 
Old 11-22-2007, 11:54 AM   #8
anon237
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

In bash it's whatever you want it to be.

Code:
#!/bin/bash

a=22
b=33

echo "\$a = $a"
echo "\$b = $b"

echo "\$a + \$b = $a + $b"
echo "a + b = $((a+b))"
The way you handle the variable determines how bash interprets it.

This guide could be of help to you. Especially this chapter: 4.3. Bash Variables Are Untyped

Hope this clears things up a bit.
 
Old 11-22-2007, 12:56 PM   #9
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Original Poster
Rep: Reputation: 17
I used like this but its not working. Its telling some error

./ram.sh: line 4: 0=free: command not found
./ram.sh: line 5: 35: No such file or directory



#!/bin/bash
clear
mem=0
$mem=free -mo | awk '/Mem/ { print $4 }'
if [ $mem < 35 ] ; then
reboot
else
mem=0
fi
 
Old 11-22-2007, 01:08 PM   #10
anon237
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Code:
#!/bin/bash
clear

mem="`free -mo | awk '/Mem/ { print $4 }'`"

if [ $mem -lt "35" ]
then
  echo "smaller"
else
  echo "larger"
fi
All the above is explained in the guide I pointed you to earlier.

BTW: matthewg42 gave very good advise (post #2). There's no need to do a reboot every time your free mem drops below a certain point.

It looks like 3/4 of total memory is used and 1/4 is still available. I'm running little to nothing at the moment and have roughly the same numbers:

Quote:
$ free -mo
total used free shared buffers cached
Mem: 4021 3197 823 0 151 2452
Swap: 3914 68 3845
 
Old 11-22-2007, 01:16 PM   #11
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Original Poster
Rep: Reputation: 17
Unhappy

Sorry but it is a Mail server.We r using Centos 4.5 with qmail. The problem is it is taking all memory then takes from swap when that too empty, It is telling "out of memory" Then it is hanging. It is not stable even 24 hours. Now using cron, We r rebooting daily at 5.00 am. To over come only i tried this way... Is there any other ways?
 
Old 11-22-2007, 01:45 PM   #12
anon237
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Quote:
Is there any other ways?
Yes; Find the underlying problem and fix it. Rebooting a mail server every 24 hours is not something you want to do.

I honestly cannot tell you more then that without knowing more. Out of memory errors can be caused by a whole range of things, from hardware that is to 'light' to handle the actual load to memory leaks to bad configuration to.........
 
Old 11-22-2007, 02:10 PM   #13
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Original Poster
Rep: Reputation: 17
Cool

Ok Sir. I will make a complete check... Thank you for your valuable help n suggessions......
 
  


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
Acer 345T Upgrade - Max RAM Limit? orzem Linux - Laptop and Netbook 4 12-20-2008 08:39 PM
Memory limit on 1G RAM machine? SlowCoder Linux - Newbie 6 03-29-2007 08:08 PM
is there a RAM limit for the kernel? goens Linux - Hardware 5 03-24-2006 02:43 AM
SCRIPT: check free space on partition base markus1982 Linux - Software 0 05-25-2003 05:52 AM
RAM limit mjsivy Linux - General 6 03-31-2001 08:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:27 PM.

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
Open Source Consulting | Domain Registration