LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Is it possible? A script to check free RAM and reboot if it is less than a limit (https://www.linuxquestions.org/questions/linux-newbie-8/is-it-possible-a-script-to-check-free-ram-and-reboot-if-it-is-less-than-a-limit-601690/)

senthilvael 11-22-2007 10:02 AM

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....

matthewg42 11-22-2007 10:11 AM

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.

senthilvael 11-22-2007 10:13 AM

Or is there any tools available for it?

senthilvael 11-22-2007 10:38 AM

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.....

senthilvael 11-22-2007 10:51 AM

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 ......?

druuna 11-22-2007 11:05 AM

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.

senthilvael 11-22-2007 11:40 AM

Ya its working. Thanks. The result is a string or an integer? How to check that...

druuna 11-22-2007 11:54 AM

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.

senthilvael 11-22-2007 12:56 PM

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

druuna 11-22-2007 01:08 PM

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

senthilvael 11-22-2007 01:16 PM

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?

druuna 11-22-2007 01:45 PM

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.........

senthilvael 11-22-2007 02:10 PM

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


All times are GMT -5. The time now is 08:43 AM.