Automate Tasks on Debian 11 - How to do it ?

There are easy to use tips and tricks which you can use to automate your daily routine and get rid of things that bother you by running your applications in the cloud.

Here at LinuxAPT, we shall look into how a beginner can work faster and more accurately on a Linux server.

What is scripting in Linux ?

Once a beginner gets used to how Linux server works then their greatest problems begin which include updating the system, adding, editing, and patching the files to keep applications running smoother.

Scripting basically helps a system admin to flawlessly work. Not only does scripting help system efficiency but also personal skills to grow as a system admin. Instead of typing out the same command again and again, it is better to automate them. It doesn’t matter which languages you use as a system admin to write your scripts. Let it be Python, Ruby, or plain shell scripts. They do one single job to help you to get repetitive tasks out of your routine.

How to automate tasks in a debian system ?

It is a fact that all Linux system admins need to keep their servers updated for security and performance reasons. This is why it is important to run apt updates often to keep our system up to date. This can be hectic when it comes to running the command again and again.

You can easily automate this and will never need to type the related commands again. You might need to change the script file if you update your repository configuration in future.

Start by adding the following script to your daily cron jobs:

$ #!/bin/bash
$ Date=date
$ sudo apt update
$ echo "apt udpate has been run at $DATE" >> /var/log/apt-updatestats

To run it, execute:

$ #!bin/bash

The output will display:

$ Date=date

From the above output, the "shebang" shows what kind of interpreter should be used for the command coming up next. This first line needs a full path to the interpreter of whatever language you are using. Here, we are using 'bash'.

The second line states that we have initialized a variable DATE which stores the current date in it:

$ sudo apt update

The third line runs the command to update the apt database.

Now the last line is as following:

$ echo "apt update has been run at $DATE" >> /var/log/apt-update stats

exports a message to our file "apt-updatestats". This helps us to keep a record of how many times have the command run already.

What to do if we have multiple commands ?

We dealt with how to automate one command but what if we have more than one command. In the following example, we will examine how to execute multiple commands and keep going faster on a Debian 11 system.

The bash shell interpreter is so powerful that it can create loops, run functions, and much more. To make use of powerful bash, we will add our multiple commands in a single file and source it.

Here is an example from /etc/init.d/ on a Debian 11 server:

$ . /lib/lsb/init-functions

As is an init script, it is easier to have all of those functions in a single file. This file is then sourced to accomplish a variety of tasks depending on your everyday use of the system.

[Need help in fixing Linux system issues ? We can help you. ]

This article covers how simple bash scripting can help us to avoid wasting our time on repetitive tasks. In fact, these tasks should run in the background and in case of any failure we simply need to look into the log files. 

Related Posts