6

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

4
  • 1
    Write a small script and run it as system service. This script can listen for some user-triggered event like touch /tmp/clear_cache, issue the command and remove the file. Be careful with that script to avoid security problems.
    – Philippos
    Commented Oct 22, 2019 at 6:53
  • That sounds like it could work if I find out how to do that. But I see one problem with this approach: the touch command returns immediately, while clearing caches can take a few seconds (I have seen more than a minute on occasion). So the user script could proceed before caches are actually clear.
    – MechEng
    Commented Oct 22, 2019 at 7:07
  • 1
    The user script should pause until the file is deleted. This indicates that the cleaning is done.
    – Philippos
    Commented Oct 22, 2019 at 7:08
  • 2
    Put the necessary commands in a (very) short script. Modify the sudoers file to allow all users to execute your script with root permissions. For bonus points have the script call itself with sudo if it's not already running as the root user. Commented Oct 22, 2019 at 7:21

1 Answer 1

5

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

#!/bin/sh
# Description
# Forces the OS to clear OS caches

# Run a sync to reduce dirty caches
sync

# 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)
sleep 5

# Reset to 0
echo 0 | tee /proc/sys/vm/drop_caches

exit

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:
sudo /path_to_file/free_os_cache.sh

That ought to get you close enough that you can tweak it for your particular environment.
$0.02

2
  • This UL post is very helpful: unix.stackexchange.com/questions/17936/…
    – Scottie H
    Commented Oct 23, 2019 at 20:25
  • 1
    Thanks for writing this up! The UL post you linked to above explains that your last echo 0 isn’t needed ;-). Also, since the script runs as root, you can redirect to the files, tee isn’t necessary. Commented Oct 24, 2019 at 7:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .