▼
Clear Memory in Linux (specially for squid servers)
☺
If you are hosting SQUID proxy server on your Linux box, you may have noticed that SQUID as a proxy server is very greedy with memory. Over the period of time, it consumes large amount of memory , even in some cases 98-99%.
This script will clear the memory so that it be used by system or SQUID again. Make sure you are logged in with root user in order to continue.
Just create a new script and put it in any folder like /temp
Create File by following command
mkdir /temp cd /temp touch clearmem.sh touch /var/log/cron.log chmod +x clearmem.sh
Now edit the file by
nano /temp/clearmem.sh
.
And Paste the following code.
#!/bin/bash # Clear/Dump Cache Memory, to increase free memory, useful for SQUID proxy OR for general purposes. # Syed Jahanzaib / Email: aacable@hotmail.com Web: https://aacable.wordpress.com # Make sure you are Logged in with user root. # Setting variables for script set $(date) time=`date |awk '{print $4}'` # Action . . . sync; echo 3 > /proc/sys/vm/drop_caches # Add entry in /var/log/cron.log to make record. echo [Jz] Cache Memory Clear, Action Logged at $6-$2-$3 $time > /var/log/cron.log
.
Save, and Exit.
Now change the permission so it can be executed.
chmod 755 /temp/clearmem.sh
.
Now first execute
free -m
and note down the free memory,
Now execute the file by running …
/temp/clearmem.sh
check the log file by
cat /var/log/cron.log
Now again check the free amount from terminal with the command
free -m
.
And you will see the changes before and after running the file clearmem.sh
.
Howto schedule this file to run daily/hourly using ‘crontab’
To run this file on hourly basis, open terminal, (make sure you are logged in with the root user.
type
crontab -e
(if it asks for text editor, select nano ,)
Now paste following code …
@daily /temp/clearmem.sh # Run Daily in night at 00:00 hours
# @hourly /temp/clearmem.sh # Enable this if you want to run the script on hourly basis
Now, based on above selection, cron job will run this command at selected scheduled timings and clear any memory cache
Now some Explanation of what above script do . . .
It will cause the kernel to drop clean caches, dentries and inodes from memory, causing that memory to become free.
* sync only makes dirty cache to clean cache. cache is still preserved.
* drop_caches doesn’t touch dirty caches and only drops clean caches. So to make all memory free, it is necessary to do sync first before drop_caches in case flushing daemons hasn’t written the changes to disk.
This is in generally a non-destructive operation.
.
Regard’s
Syed Jahanzaib
Sir………this configuration required new squid installation or running bases
LikeLike
Comment by Salman — January 10, 2014 @ 3:16 PM
It can be used at any linux base OS, either squid or other, either fresh or old 🙂
LikeLike
Comment by Syed Jahanzaib / Pinochio~:) — January 11, 2014 @ 3:10 PM
I really liked the picture !!!! 😀 .. nice work
LikeLike
Comment by ammar — January 11, 2014 @ 11:11 AM
thank you bro , it`s really help with squid cache server coz I used 16G of ram after 24 hrs no more free memory
LikeLike
Comment by alfanet1 — January 11, 2014 @ 1:44 PM
Sir………..how to run this file every 12:00 hours after ?
ALLAH aap ko jaza e khair deain……!
LikeLike
Comment by Salman — January 13, 2014 @ 2:17 PM
Sir………..how to run this file every 12:00 hours after ?
At 12:00 am & again 12:00 pm
ALLAH aap ko jaza e khair deain……!
LikeLike
Comment by Salman — January 14, 2014 @ 2:26 PM
hello bro , how to run this script every 2 hrs ?
coz every 24 too much and my rams almost used by squid
LikeLike
Comment by alfanet1 — January 16, 2014 @ 10:15 PM
Sir…………is this correct commad for crontab every 12 hours……?
* */12 * * * /temp/clearmem.sh
LikeLike
Comment by Salman — January 19, 2014 @ 3:19 AM
Salam Alikum
thank you bro
LikeLike
Comment by alfanet1 — January 19, 2014 @ 2:17 PM
Sir………………its good working for crontab every 12 hours
* */12 * * * /temp/clearmem.sh
Thanks a lot of you Jahanzaib Bhai……………!
ALLAH AAP KO JAZA E KHAIR DEY………….!AAMEEN
LikeLike
Comment by Salman — January 20, 2014 @ 4:47 PM
Dear Sir,
I need ur help, can we install mikrotik on IBM x3550 m4 server ?
LikeLike
Comment by Fawad Ahmad — January 23, 2014 @ 1:49 PM
Yes you can. But As far as I know, Mikrotik dont support SCSI disks.
You can also use virtualization to host mikrotik along with other OS in single box. its more convenient and good for management and backup purposes.
LikeLike
Comment by Syed Jahanzaib / Pinochio~:) — January 27, 2014 @ 8:48 AM
Reblogged this on Henri Sekeladi – Sebuah Catatan Perjalanan and commented:
Add your thoughts here… (optional)
LikeLike
Comment by rihend — January 29, 2014 @ 5:55 PM
i use it really WOW thanks ………
LikeLike
Comment by Mostafa Mohamed — January 30, 2014 @ 2:20 AM
thanx
really working and helpful
LikeLike
Comment by ali sameer — March 19, 2014 @ 1:24 AM
Sir……..How to Auto Clear Cache by schedule of Squid Proxy Server ?
kindly..! Sir..write on article with script…….!
ALLAH aap ko jaza e khair dey……..
LikeLike
Comment by Salman — January 22, 2015 @ 1:31 AM