Automating Ubuntu Routine Maintenance using CRONTAB

· 1 min read
Automating Ubuntu Routine Maintenance using CRONTAB
Photo by Gabriel Heinzer / Unsplash

A cron job is a Linux command used for scheduling tasks to be executed sometime in the future with a pre-decided time and date pattern. This is normally used to schedule a job that is executed periodically.

We will set up a rule that will tell the Ubuntu cloud server when to execute a set of pre-defined commands. These commands basically update, upgrade and also clean and remove the old.

Login into your Cloud Server SSH client and edit the crontab file

sudo nano /etc/crontab

Insert the below Ubuntu maintenance after all other existing rules. It will execute the CRON job at the set daily interval at 01:25 server time.

25 1 * * * root sudo -- sh -c 'apt-get update; apt-get upgrade -y; apt-get dist-upgrade -y; apt-get autoremove -y; apt-get autoclean -y'

The Ubuntu cloud server is pretty powerful and does not require frequent reboots. However, it is a good idea to reboot the server once a week or at least whenever you notice the alert saying *** system reboot required *** after login into the SSH client.

You can even automate the periodical reboot using the CRON jobs. All you need is to add the following snippet in the crontab file for a weekly reboot on Saturday at 05:25 hours server time.

25 5 * * 6 root sudo reboot

Once you’ve added the above rules to the crontab file, do not forget to reboot the cloud server using the below command: sudo reboot

Cron is an essential Linux utility designed for managing and scheduling jobs for your system. They are widely used to make repetitive jobs on your system automated and perform system maintenance related jobs.