2

I'm trying to clear memory cache using cron.

Cron file:

*/2 * * * *  ~/clear_mem.sh

clear_mem.sh:

#!/bin/sh
sudo sh -c "sync; echo 3 > /proc/sys/vm/drop_caches"
touch f

File f is created every two minutes. But memory isn't cleared. If I call script by hands everything is fine. Why?

1
  • This begs the question... why are you trying to clear memory cache? – heynnema Aug 1 '16 at 13:44
3

Because cron runs commands non-interactively, there is no way for sudo to authenticate.

For commands that require root permission, you should use root's crontab (i.e. sudo crontab -e instead of plain crontab -e) and then avoid the use of sudo within your commands and scripts altogether.

You should also give the full path to the script - preferably locate it some systemwide e.g. /usr/local/bin/clear_mem.sh in place of ~/clear_mem.sh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.