Video, automating and scheduling tasks. In this video, we will cover basic shell scripting to automate tasks and scheduling of tasks using crontab. It is essential to know about shell scripting to automate tasks in Raspberry Pi four. A shell is a special user program that provides an interface to the user to use operating system services. Shell accepts human readable commands from the user and convert them into something which the kernel can understand. It is a command Language Interpreter that executes commands from input devices, such as keywords or from files.
The shell gets started when the user logs in or starts the terminal. A shell is kind of like a spell that you come up with on the spot to solve some problem. Then scripts are a lot like the pages of a spell book, where you keep the incantations That you found really useful. To speak with a shell, a special programming language, called bash was invented. shell scripts allow you to automate pretty much anything you can do on the Linux command line. A shell script is simply a text file containing a series of shell commands that are executed one by one, as if they were input directly on the command line.
They can save time if you need to execute lots of commands at once. Or if you want to execute commands automatically, like when the pipe boots up. The shell language is a high level programming language. It's more removed from system and memory than lower level languages like assembly or C. This basically means that it has commands and functions to perform tasks that would otherwise take a lot of code to execute. So what can a shell script do? shell scripts are great if you need to enter long sequences of commands into the command line to do something.
Most operations can be accomplished with a single command. If you know how to write a shell script for read. For example, they can be used to control what happens when the computer boots up. Start applications. When an event occurs, use the output of one command as the input for another command, batch, rename and move files. batch convert file formats.
One handy feature of a shell script is the ability to create pipes. A pipe allows the output of one command to be forwarded to the input of the next command. Pipes can be used with as many commands as you want. shell scripts are simply an executable txt file with extension.sh. In this example, we'll write a simple Hello World. script to demonstrate how to create and run them.
To begin, log into your Raspberry Pi and navigate to the directory where you want to save the script. Then open the nano text editor and create a new file named Hello hyphen world.sh. By entering this the command prompt sudo nano Hello hyphen world.sh. Now enter this code into the text editor. The first line of this program slash exclamatory forward slash bin forward slash sh is called a shebang. This test the bash shell to execute the commands in the script.
Every script shell you create will need this on the first line of the script. Exit and save the file in nano by pressing Ctrl X to save and exit. Next we will need to make the helloworld.sh file executable to do that To this at the command prompt sudo ch mod plus x Hello hyphen world.sh. Now that the shell script has been made executable, we can run it. Navigate to the directory where the file is saved and enter either of this, the words hello world will be printed to the line below the command prompt. This hello world script isn't particularly useful, but it will show you the basics of how to create and run a shell script.
Next, shall we look at how to schedule a task? Well, you will have to use a tool called crontab that allows you to launch tasks with a user you want when starting the raspberry or whenever you want. cron is a service automatically started at each boat of the Raspberry Pi, which allows the user to execute shell commands. What we are going to see today is how to tell cron to enter execute our command or script when needed. A cron tab is a tool that will allow us to list what we want to start in a format understandable by the cron service. A cron tab will contain two things, the list of commands to run, when to run them.
Crontab is also a command. Here's the syntax to open the crontab crontab hyphen E. First thing, you have to choose an editor, I advise you to stay on nano. So keep the default choice and hit enter. That's it. You're now in the editor of crontab and can be a little intimidating if it's the first time you access it. I'll explain what to do.
First of all, all the lines starting with a hash arguments, they do nothing and we can ignore them. Our changes will be made at the end of the file. You are probably wondering what the five stars mean? The syntax of an entry in the crontab is as follows. Now that you understand the theory, let's look at a simple example. To be sure it's clear.
Imagine that you want to run the Hello World script. Every wetness day at midnight. You must add a line like this 00 asterisk asterisk, three forward slash home, forward slash pi forward slash desktop, forward slash Hello world.sh. There are a lot of flexibility with shadowing in crontab like launch script at fixed hours. Start a script during a fixed interval. Schedule a script only during the weekdays.
You can also start something on boot. Example scripts for all the above cases are given in the resources. Now I will give you an activity you have to create a shell script to do apt hyphen, update, and apt hyphen upgrade Use crontab to make it start every time on board. This will make sure that your Raspberry Pi will stay updated without manual intervention. Summary In this video we have covered the following topics, basic shell scripting to automate tasks, scheduling tasks using crontab section summary. In the section we have covered the following topics, basics of Linux, basic Linux commands, advanced Linux commands, automating and scheduling tasks.
In the next section, we will learn basic python programming