Debian

How To Automate Tasks on Debian 11

How_To_Automate_Tasks_on_Debian_11

In this article, we are going to discuss how a beginner can work faster and more accurately on a Linux server. I will share simple and easy-to-use tips and tricks so you can automate your daily routine and get rid of things that bother you by running your applications in the cloud. I will be using Debian 11 Server Edition for this guide. You can use the same or any Debian variant server including Ubuntu. All instructions and commands work without any issue.

Scripting

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.

Here comes scripting. It 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?

Let’s see how things work when we automate something.

It has been observed that all Linux system admins need to keep their servers updated for security and performance reasons. As we are working with Debian 11 server, we need 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.

Let’s add 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

Let’s see how this script works:

$ #!bin/bash

This first thing is called “shebang”. It 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. We are using bash in our case.

$ DATE=date

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 if we have multiple commands?

We learned 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/hwclock.sh on a Debian 11 server:

$ . /lib/lsb/init-functions

As hwclock.sh 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.

Conclusion

In this article, we have covered how simple bash scripting can help us to avoid wasting our time on repetitive tasks. These tasks should run in the background and in case of any failure we simply need to look into the log files. We have covered both single-line commands, and how to use files containing multiple commands for bash interpreters. I will be sharing some advanced scripting techniques in the next article. If you have any questions, we are here at LinuxWays to help you out.

Similar Posts