How to clear system page and buffer cache daily on linux server ?

In this article, we will see how to clear the system page and buffer cache daily on our linux system.

So the idea is to create a script and put it in our cron daily schedule location.

Lets see what we need to do step-wise.

1. Create the new script file in cron.daily.

[root@ngelinux001 ~]# cd /etc/cron.daily/
[root@ngelinux001 cron.daily]# ls -ltr
total 24
-rwxr-xr-x. 1 root root 2239 May 22  2015 certwatch
-rwxr-xr-x. 1 root root  618 May 22  2015 man-db.cron
-rwxr-xr-x. 1 root root  744 Aug 16  2016 snapper
-rwx------. 1 root root  219 Sep 25  2017 logrotate
-rwx------. 1 root root  208 Nov  9  2017 mlocate
-rwxr-xr-x. 1 root root  434 Nov 27  2017 0logwatch
[root@ngelinux001 cron.daily]# touch clearcache
[root@ngelinux001 cron.daily]# chmod 755 clearcache
[root@ngelinux001 cron.daily]#

 

2. Now edit the file and put these contents.

#!/bin/bash
echo 3 > /proc/sys/vm/drop_caches

 

3. Now check buffer cache usage and run the script.

[root@ngelinux001 cron.daily]# free -g
              total        used        free      shared  buff/cache   available
Mem:            255           6         122           0         126         210
Swap:            15           0          15

[root@ngelinux001 cron.daily]# ./clearcache

[root@ngelinux001 cron.daily]# free -g
              total        used        free      shared  buff/cache   available
Mem:            255           6         244           0           3         228
Swap:            15           0          15
[root@ngelinux001 cron.daily]#

As we can see the memory is now free from the buffer.

Now this script gets scheduled daily to run alongwith cron.

Just make sure the cron service is running fine.

[root@ngelinux001 cron.daily]# service crond status
Redirecting to /bin/systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2022-06-10 13:43:14 EDT; 4 days ago
 Main PID: 2769 (crond)
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments