Clear memory cache using Crontab

6,143

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

Share:
6,143

Related videos on Youtube

Long Smith
Author by

Long Smith

Updated on September 18, 2022

Comments

  • Long Smith
    Long Smith over 1 year

    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?

    • Boris Hamanov
      Boris Hamanov over 7 years
      This begs the question... why are you trying to clear memory cache?