How to do a GNU / Linux Maintenance using a Script?

Maintenance and Update Script on GNU / Linux

Maintenance and Update Script on GNU / Linux

To maintain operability and minimize errors and failures, it is good to maintain our GNU / Linux Operating System using a Script, since it allows us via «Cron» to program it for its periodic execution.

For this reason, in this publication we are going to try to expose the most elementary steps (command orders) to be inserted into our maintenance script to create / modify, whether they are essential or recommended to keep our PC in correct operation.


Much of the problems (technical difficulties) that arise in the Operating Systems, whether Private or Free they can be avoided or prevented if proper periodic maintenance (preventive / corrective) is carried out.

We bear in mind that operating systems are very robust systems and that is why they tend to present certain flaws or technical difficulties, due to normal or exaggerated uses by the user, updates and installations of the same or the applications installed within it.

These are usually repaired or eliminated, many times by technical personnel with graphic applications or in extreme cases by formatting the Operating System. But many times, a scheduled (automated) maintenance can save us many hours / labor on the part of the technical personnel in charge of it.

And in the case of GNU / Linux Operating Systems this is very easy to do through a Script programmed within the «Cron» System using the Terminal.

Preventive Maintenance of GNU / Linux Operating Systems

What tasks should a Maintenance Script execute?

A good maintenance script should meet some of the following requirements or parameters to maintain the operability of our GNU / Linux Operating System, which:

  1. Run as root or with Superuser permission: Since the maintenance of any Operating System must involve certain critical or sensitive tasks that only a user with Administrator permissions must be able to carry out.
  2. Update the package lists in our respective repositories: To keep the System and the User himself from possible updates, critical or not, available.
  3. Perform secure updates to the packages available in the repositories: To keep the System as updated as possible with zero risk of package problems.
  4. Solve possible present or future parcel problems: That may have been present or have yet been submitted in a secure update.
  5. Debug the Operating System as much as possible: By eliminating the applications, installation files, configuration, language or error log, not used or necessary within the Operating System to make better use of our existing disk space.
  6. Update our Grub Boot System: In the event that there have been undetected changes in it, such as a change in kernels, names of the boot records or detection of new Operating Systems within the Disk.
  7. Update our Plymouth Starting System: In case there have been undetected changes in it, such as a change in the theme or image shown during the loading of the Operating System.
  8. Display vital information about Equipment Status: Mainly about the Distribution of Disk Space and the File System. Although the display of parameters such as: Memory, CPU, Disk, Video, Kernels, Network Interfaces, among others, could be programmed.

Command Orders for Maintenance Script

What command commands must a Maintenance Script execute?

Below are some of the command commands that may be useful or necessary to create a good maintenance script on your GNU / Linux Operating System:

#!/usr/bin/env bash

set -eou pipefail
setterm --reset

setterm -background red

if [[ "$(id -u)" != "0" ]]; then
sleep 3
sleep 3

sudo apt update; sudo update-apt-xapian-index; sudo aptitude safe-upgrade; sudo apt install -f; sudo dpkg --configure -a; sudo apt --fix-broken install

sudo localepurge; sudo update-grub; sudo update-grub2; sudo aptitude clean; sudo aptitude autoclean; sudo apt-get autoremove; sudo apt autoremove; sudo apt purge; sudo apt remove

sudo rm -f /var/log/*.old /var/log/*.gz /var/log/apt/* /var/log/auth* /var/log/daemon* /var/log/debug* /var/log/dmesg* /var/log/dpkg* /var/log/kern* /var/log/messages* /var/log/syslog* /var/log/user* /var/log/Xorg* /var/crash/*

sudo update-initramfs -u

sudo df -h

sudo du -hs /* | sort -k 2

sudo dpkg-query -Wf='${Installed-Size} ${Package}\n' | sort -n

sudo echo "" > ~/.bash_history

You can complement this simple script with your preferred command commands, or adapt them to their own GNU / Linux Distribution in order to keep their respective Operating System up to date.

In case you want to know a little more about what to do to optimize your GNU / Linux Operating System we recommend this other blog post: Post Installation Guide DEBIAN 8/9 - 2016 - Part I or this other external forum post: Cleaning and Maintenance in Linux from the Terminal

I hope you like this post and it is useful as usual!

The content of the article adheres to our principles of editorial ethics. To report an error click here!.

8 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *



  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   juliosao said

    The script can be useful, but a note, where you delete the logs, it could be better to use "logrotate", it is a very good tool to manage logs, because sometimes deleting the logs, especially if they are recent, is not a very good idea :

    This utility is capable of deleting old logs, configuring whether before they must be renamed, compressed, indicate how often they must be deleted, etc. Also that I know is in almost all distributions, I have tested it in centos, ubuntu and debian, always satisfactorily.

    A greeting.

  2.   Marc said

    The question for me is… How dangerous are these commands for Linux Kubuntu 18.04? Is there a risk of losing something useful or misconfiguring something? Questions from someone who barely knows Linux distributions and I have had to deal with reinstalls for gaffes. Thank you.

  3.   Ing. Jose Albert said

    You're right! Although logrotate is more for servers! And if you use the Script for Users' PC, it is easier to keep that line to delete the Logs that sometimes due to mental laziness or lack of technical experience, Linux leaves sending errors in the background that make the system boot collapse due to lack of disk space unexpectedly!

    For that reason, to the taste of the implementer, it remains to add or remove lines according to the type of user and team where it is executed.

  4.   Ing. Jose Albert said

    Nothing dangerous! Once you run them manually the first time to see that everything is fine, leaving them programmed will hardly break your system!

  5.   Frank Davila said

    It works for Arch Linux?

    1.    Ing. Jose Albert said

      Of course, the only thing is that maybe the syntax of the commands changes since I think that in the Distros not based on DEBIAN certain commands or package names change slightly the way they are executed! But Scripts are universal on all Linux Systems.

  6.   Reinaldo gonzalez said

    Good morning, greetings I am new, in this linux distro now I have linux mint 18 sarah, with i386 architecture, seeing this post, I ask how I execute that Script, with what name is it saved or placed to run the Script? pc is intel p4, 2gb ram, 320 hdd disk, nvidia g7200 card, but sometimes it hangs,

    steps to run this script?

    Please excuse me if I asked the wrong questions
    all information will be of great help thanks….

    1.    Linux Post Install said

      Greetings Reinaldo! You could make an alias in the .bashrc of your user or root as follows:

      alias yournamealias = 'bash /path/'

      and then in your terminal just write: yournamealias

      You can then load it into a cron to make it periodic, which I don't recommend, or run it manually just by typing:

      bash /path/