{"id":16738,"date":"2022-04-22T19:53:41","date_gmt":"2022-04-22T19:53:41","guid":{"rendered":"https:\/\/linuxways.net\/?p=16738"},"modified":"2022-04-22T19:53:41","modified_gmt":"2022-04-22T19:53:41","slug":"how-to-automate-tasks-on-debian-11","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/debian\/how-to-automate-tasks-on-debian-11\/","title":{"rendered":"How To Automate Tasks on Debian 11"},"content":{"rendered":"<p>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.<\/p>\n<h2>Scripting<\/h2>\n<p>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.<\/p>\n<p>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\u2019t 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.<\/p>\n<h2>How to automate?<\/h2>\n<p>Let\u2019s see how things work when we automate something.<\/p>\n<p>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 <strong>apt updates<\/strong> often to keep our system up to date. This can be hectic when it comes to running the command again and again.<\/p>\n<p>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.<\/p>\n<h3>Let\u2019s add the following script to your daily <strong>cron<\/strong> jobs:<\/h3>\n<pre><strong>$ #!\/bin\/bash<\/strong>\r\n\r\n<strong>$ Date=date<\/strong>\r\n\r\n<strong>$ sudo apt update<\/strong>\r\n\r\n<strong>$ echo \u201capt udpate has been run at $DATE\u201d &gt;&gt; \/var\/log\/apt-updatestats<\/strong><\/pre>\n<h3>Let\u2019s see how this script works:<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"484\" height=\"198\" class=\"wp-image-16739\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-177.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-177.png 484w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-177-300x123.png 300w\" sizes=\"auto, (max-width: 484px) 100vw, 484px\" \/><\/p>\n<pre><strong>$ #!bin\/bash<\/strong><\/pre>\n<p>This first thing is called \u201cshebang\u201d. 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 <strong>bash<\/strong> in our case.<\/p>\n<pre><strong>$ DATE=date<\/strong><\/pre>\n<p>The second line states that we have initialized a variable <strong>DATE<\/strong> which stores the current <strong>date<\/strong> in it.<\/p>\n<pre><strong>$ sudo apt update<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"668\" height=\"332\" class=\"wp-image-16740\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-178.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-178.png 668w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-178-300x149.png 300w\" sizes=\"auto, (max-width: 668px) 100vw, 668px\" \/><\/p>\n<p>The third line runs the command to update the <strong>apt<\/strong> database.<\/p>\n<p>Now the last line is as following:<\/p>\n<pre><strong>$ echo \u201capt update has been run at $DATE\u201d &gt;&gt; \/var\/log\/apt-update stats<\/strong><\/pre>\n<p>exports a message to our file \u201c<strong>apt-updatestats<\/strong>\u201d. This helps us to keep a record of how many times have the command run already.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"665\" height=\"50\" class=\"wp-image-16741\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-179.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-179.png 665w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-179-300x23.png 300w\" sizes=\"auto, (max-width: 665px) 100vw, 665px\" \/><\/p>\n<h2>What if we have multiple commands?<\/h2>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>Here is an example from <strong>\/etc\/init.d\/hwclock.sh<\/strong> on a Debian 11 server:<\/p>\n<pre><strong>$ . \/lib\/lsb\/init-functions<\/strong><\/pre>\n<p>As <strong>hwclock.sh<\/strong> is an <strong>init <\/strong>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.<\/p>\n<h2>Conclusion<\/h2>\n<p>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.<\/p>","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>","protected":false},"author":1,"featured_media":16795,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[976,521],"class_list":["post-16738","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-debian","tag-automation","tag-debian-11"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/16738","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=16738"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/16738\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/16795"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=16738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=16738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=16738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}