1

I have a script called clearcache.sh to clear my buffers / cache but when I run it keeps saying Permission denied.

Whats in the script: clearcache.sh

#!/bin/bash
sync && echo 1 | tee /proc/sys/vm/drop_caches

Rights:
-rwxr-xr-x 1 rt rt 464 Dec  5 02:10 clearcache.sh

Image


How I run it to test it:

rt@sys76:~/clearcache$ ./clearcache.sh 
tee: /proc/sys/vm/drop_caches: Permission denied
1

I want to place this in a chron job but I need to get past this permission issue:

Update: Here's the drop_caches file and it's rights owned by root what's the correct way to run this from a chron job using a script? PS: I'm running Ubuntu 20.04

image2

3
  • 1
    What are permissions of /proc/sys/vm/drop_caches? A regular user is probably not allowed to write to that file.
    – danzel
    Dec 5, 2020 at 7:33
  • @danzel it looks like root is the owner of /proc/sys/vm/drop_caches. I add the image to the question. Is there a way to work around this?
    – Rick T
    Dec 5, 2020 at 7:35
  • Separate to everything else, there is almost never a good reason to actually do this (Linux does a solid job of managing this for you, and you're discarding performance improvement), and the rare good reasons are when you're about to run a streaming processing job, not on cron. Dec 5, 2020 at 15:53

1 Answer 1

5

root & perms

I guess you run your script with root (?)
In any way, you should not modify the access rights of the file

no need to tee

You dont need to tee, you'd better use sysctl

sudo sync && sudo sysctl vm.drop_caches=1

pagecache, dentries & inodes

vm.drop_caches=1 # to free pagecache
vm.drop_caches=2 # to free dentries & inodes
vm.drop_caches=3 # to free pagecache, dentries & inodes

So why not use vm.drop_caches=3

cron schedule

As you want to schedule it with cron, you can design your script for the root user, then schedule it by inserting a job in the root's crontab

The script

#!/bin/bash
/usr/bin/sync
# /usr/bin/echo 1 | /usr/bin/tee /proc/sys/vm/drop_caches
/usr/sbin/sysctl vm.drop_caches=1

Put the script file in a directory available to root user. For ex in the /root folder, ie /root/clearcache.sh

Then add a root cronjob
Here, the script will be run every hours, 0 minutes, every day

sudo crontab -e
0 * * * * /root/clearcache.sh

Finally
A script is not required, simply add the two commands as a job in the root's crontab

sudo crontab -e
0 * * * * /usr/bin/sync; /usr/sbin/sysctl vm.drop_caches=1
3
  • Thanks!. I read an article to not use 3 in production it may cause a crash. (I was just playing it safe) tecmint.com/…
    – Rick T
    Dec 5, 2020 at 8:23
  • 1
    "As sync must be run by root" - sync does not require root, so your conclusion was incorrect there.
    – marcelm
    Dec 5, 2020 at 19:47
  • This worked for me (I'm using Ubuntu 20.04) 0 * * * * /bin/sync; /sbin/sysctl vm.drop_caches=1
    – Rick T
    Dec 18, 2020 at 2:44

You must log in to answer this question.

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