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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
11-22-2007, 10:02 AM
|
#1
|
Member
Registered: Nov 2007
Posts: 73
Rep:
|
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....
|
|
|
11-22-2007, 10:11 AM
|
#2
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
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
|
|
|
11-22-2007, 10:13 AM
|
#3
|
Member
Registered: Nov 2007
Posts: 73
Original Poster
Rep:
|
Or is there any tools available for it?
|
|
|
11-22-2007, 10:38 AM
|
#4
|
Member
Registered: Nov 2007
Posts: 73
Original Poster
Rep:
|
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.....
|
|
|
11-22-2007, 10:51 AM
|
#5
|
Member
Registered: Nov 2007
Posts: 73
Original Poster
Rep:
|
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 ......?
|
|
|
11-22-2007, 11:05 AM
|
#6
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
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.
|
|
|
11-22-2007, 11:40 AM
|
#7
|
Member
Registered: Nov 2007
Posts: 73
Original Poster
Rep:
|
Ya its working. Thanks. The result is a string or an integer? How to check that...
|
|
|
11-22-2007, 11:54 AM
|
#8
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
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.
|
|
|
11-22-2007, 12:56 PM
|
#9
|
Member
Registered: Nov 2007
Posts: 73
Original Poster
Rep:
|
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
|
|
|
11-22-2007, 01:08 PM
|
#10
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
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
|
|
|
|
11-22-2007, 01:16 PM
|
#11
|
Member
Registered: Nov 2007
Posts: 73
Original Poster
Rep:
|
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?
|
|
|
11-22-2007, 01:45 PM
|
#12
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi,
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.........
|
|
|
11-22-2007, 02:10 PM
|
#13
|
Member
Registered: Nov 2007
Posts: 73
Original Poster
Rep:
|
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 01:27 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|