3

I want to run this command:

free && sync && echo 3 > /proc/sys/vm/drop_caches && free -m

by cron. That way I can free my RAM and cache in an Ubuntu system.

I'm using these steps:

# crontab -e
2 * * * * my_user_name /usr/bin/free free && sync && echo 3 > /proc/sys/vm/drop_$

save & exit

# service cron restart

But, when I run free -m nothing changes.  For reference, it looks something like this:

# free -m
             total       used       free      shared    buffers     cached
Mem:          7976       5064       2911           5        169       3478
-/+ buffers/cache:       1416       6560
Swap:         8112       1264       6848

1 Answers1

3

I'm not sure why you want to drop the caches every hour - that is going to kill performance.

There are a few problems with what you've done:

  1. You need to edit the crontab of root, as you cannot write to /proc/sys/vm/drop_caches as a non-root user.
  2. Your crontab entry is not correct. It should be:

    2 * * * * /usr/bin/free && sync && echo 3 > /proc/sys/vm/drop_caches
    

    The fields, in order, are: minute, hour, day of month, month, day of week, command. You don't need to restart cron after editing a crontab.

mjturner
  • 7,300
  • Isn't it 2 minutes after every full hour? Every 2 minutes would be */2 – chaos Aug 12 '15 at 07:36
  • @chaos Whoops, yes, you're right! Answer amended – mjturner Aug 12 '15 at 07:37
  • Dear sir, My system have 8 GB RAM and 128MB Graphic card but system goes to block screen. here i check that cpu usage are 60 -85 percent and RAM free space 130MB left. that way i drop cache for increase free space of RAM If i am wrong please suggestion me for this – Chander Kant Aug 12 '15 at 07:41
  • @ChanderKant The fact that the operating system is using all your RAM is a good thing - it will be caching frequently used files, etc. If you clear the caches all of that will be lost and what was previously cached will need to be loaded from disk. Rather let the operating system do what it does best - manage your system. See, for example this question. – mjturner Aug 12 '15 at 07:48
  • Dear Mjturnersir, need to upgrade hardware of my system and -/+ buffers/cache :- used 1909 free 6067 can you help me on this topic what is the use of it – Chander Kant Aug 12 '15 at 08:21
  • @ChanderKant What are you using your machine for? Do you have a swap file - that can help in low memory situations (and it's far better for your system to page than for you to clear caches every hour). – mjturner Aug 12 '15 at 09:04
  • i have create swap partition that have created on ubuntu installation time swap details of root@www:~# free -m total used free shared buffers cached Mem: 7976 5064 2911 5 169 3478 -/+ buffers/cache: 1416 6560 Swap: 8112 1264 6848 – Chander Kant Aug 12 '15 at 10:15