Score:0

How to create an auto executeable for running commands ad admin?

mh flag

I have an old laptop I run Ubuntu on. However the capacity of this pc is small, so I have to clean often. But opening the file with the commands, copying and pasting into the terminal, gets boring quick. So the idea is to set up a cron job every 5 hours. But I can't even get the .sh file to execute my admin tasks, even though I gave it chmod 777.

The commands in my clean.sh:

#!/bin/bash
free -h && sudo sysctl -w vm.drop_caches=3 && sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches && free -h
echo 3 > /proc/sys/vm/drop_caches && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'
sync; echo 2 | sudo tee /proc/sys/vm/drop_caches 
sync; echo 1 | sudo tee /proc/sys/vm/drop_caches 
apt-get purge $( dpkg --list | grep -P -o "linux-image-\d\S+"| head -n-4 )
apt-get autoremove
apt-get clean
sync && echo 3 | sudo tee /proc/sys/vm/drop_caches***
update-grub
apt-get autoremove --purge
apt-get remove package-name1 package-name2
apt-get autoremove

How do I do this so that it executes these lines of commands?.

Marco avatar
br flag
One mistake and you kill your installation. Most of the stuff is not necessary and the rest is dangerous. Dropping caches and cleaning swap slows down the system, linux handles this perfectly by itself. With autoremove all unneeded kernels are removed in a secure way, your script will remove kernels in use (worst case).
Score:1
cn flag

To run a script with root permissions on Ubuntu, you can use sudo crontab -e to add the script to the system crontab file.

The script you intent to execute should be executable, i.e., have the executable bit set. For security, it is preferred to set the executable bit for the owner only, and change the owner of the script to root so that only root can execute it.

Score:1
cn flag
  • 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.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.