From the command line, three of these commands will not run as a standard or sudo user, it will only run as su, the fourth command will run as either su or sudo how can I do this. I believe if I run the script as an icon on the desktop it will present me with a cli to enter the password but if I wanted to run this as a cronjob in the middle of the night how can I do that?

I have a very basic understanding of scripting, so simple plain English answer please

Didn't look at anything else, but the names of scripts in /etc/cron.daily (and hourly, weekly and monthly) may only contain letters, digits, dashes and underscores. So anything.sh won't work because of the dot.

Also, don't assume the PATH variable has a useful value, so don't use (e.g.) swapoff but /sbin/swapoff

due to security reason, directly use echo command won't work (you can run the command from terminal but not from a script). I recall there was a trick to make it work but I forgot what was it. as an alternative you can use sysctl command. "sysctl -w vm.drop_caches=3" should give similar result. also, you only need to pick one of them since the number represent how deep you clear the memory (i.e: level 3 clears anything). if you want to go to level 3, no need to go step by step since level 3 also clears what level 1 and level 2 did.

the kernel actually will clean the memory by itself. unless you have limited ram size, you never have to clean the memory by yourself. also, tweaking the vm parameter actually considered better. you can tweak either the size, timeout, and the dirty memory ratio.

Hi Kurotsugi, thanks for the information, I had already realised the various level of the cache clearance and had reduced the script to one line using level 2.

I tried to get a script to run using "sysctl", with out any luck. I did a search on the net but everything I found about clearing the memory caches all revolved around using echo. I actually found a script that runs under sudo and use "echo" Now I don't know enough about bash scripting to know if this is a good or bad thing but I know if I run it from the cli it does exactly the same as my script does (although with added commentary).

this command should be executed manually. you can also put it under sysctl.conf and run "sudo sysctl -p" to execute it. "echo 3 | sudo tee /proc/sys/vm/drop_caches" this one should works too. if you want to make it better, you can add logs into it so that you could check the result.

I made this one specifically for android device. it has logs and auto selection feature (i.e: it will automatically select the level based on memory usage). you'll need to define the treshold value and change the log mechanism.