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.... |
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. |
Or is there any tools available for it?
|
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..... |
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 ......? |
Hi,
Free uses a space as delimiter, not a tab. If you want the bold part only: Code:
total used free shared buffers cached This awk snippet prints the third field (print $3) only if Mem (/Mem/) is present in that line. Hope this helps. |
Ya its working. Thanks. The result is a string or an integer? How to check that...
|
Hi,
In bash it's whatever you want it to be. Code:
#!/bin/bash This guide could be of help to you. Especially this chapter: 4.3. Bash Variables Are Untyped Hope this clears things up a bit. |
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 |
Hi,
Code:
#!/bin/bash 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:
|
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?
|
Hi,
Quote:
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......... |
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. |