Clearing linux caches without root privileges
In order to clear caches, I usually invoke
# echo 3 | tee /proc/sys/vm/drop_caches
This requires root privileges. Why do I need to do this occasionally? Because it can have a huge impact on the runtime of some of my programs.
My question is: how can I do this without superuser privileges, in particular these 2 circumstances:
1) I/the user does not have root privileges. Can the system administrator do anything to allow normal users run the command above?
2) The calls to drop caches are from within a shell script, and running the whole script with superuser privileges is not an option. For example because there are multiple instances of calling “mpirun” within this script, which really should not be run as root. And running everything within the script as root, although only a few lines actually need it, does not seem like good practice to me.
edit: forgot to mention: above command only works while being logged in as root. Otherwise the command would be
echo 3 | sudo tee /proc/sys/vm/drop_caches
I’ll take a stab at this answer. The OP did not say what OS is being used, so I will be somewhat generic.
First, make a file called free_os_cache.sh
# Forces the OS to clear OS caches
# Run a sync to reduce dirty caches
# Tell the OS to not make warnings
echo 4 | tee /proc/sys/vm/drop_caches
# Tell the OS to clear caches
echo 3 | tee /proc/sys/vm/drop_caches
# Wait a tiny bit, just for safety (may not be necessary)
# Reset to 0
echo 0 | tee /proc/sys/vm/drop_caches
Then, allow it to be executed
chmod 755 free_os_cache.sh
Next, add this to your sudoers file:
ALL = /path_to_file/free_os_cache.sh
Now, your users ought to be able to run this command:
That ought to get you close enough that you can tweak it for your particular environment.