chmod 777
is bad. 750 at most if you have 2 or more ppl on that system otherwise 700.
- don't put
sudo
inside scripts. Use sudo to run the script. If you need 'root' you need a sudo -i
first to get a #
. Or use /etc/crontab
with user 'root'.
- if you use cron don't put commands in the script that only echo results (like
free -h
). Otherwise you will need to redirect it to a file so you can see what the result was.
But I can't even get the .sh file to execute my admin tasks,
it is executed with
./clean.sh
and you will need to examine errors it shows. Some of these commands look off; I would assume the sysctl
command needs root not sudo.
If you want to do this using a cron add the directory of the file between ./
and clean.sh
.
In /etc/crontab
it would be something like this:
0 5,10,15,19,23 * * * root ./dir/dir/clean.sh
and it runs at 5, 10 am, 3, 7 and 11 pm.
You can add a redirection to log notifications by adding >> /var/log/clean.log
.
Personally I would not use cron but systemd for this and add a bit of logic: only execute this if the system reaches a threshold: if free -h
shows used memory above 90% or so then execute these lines. That way the system is cleared when needed.
Mind also that most of these do nothing to address "However the capacity of this pc is small".
These don't need to run every 5 hours:
apt-get purge $( dpkg --list | grep -P -o "linux-image-\d\S+"| head -n-4 )
apt-get autoremove
apt-get clean
apt-get autoremove --purge
apt-get remove package-name1 package-name2
apt-get autoremove
And why is there an update-grub
? You don't alter grub in this script.