Linux Foundation LFCS – Domain No. 2 – Operation of running system Part 2

  1. system

So let’s talk about starting the system D process. We are still in the booting section of Linux system and we are going through different steps of it. So the kernel starts the system D process with the process ID of PID one. As we discussed in the last lecture, system D is the first process that starts after the system boots and is the final process that is running when the system shuts down. It controls the final stages of booting and prepares the system for use. It also speeds up booting by loading services concurrently. Running system D reads the file linked by system fault target and as an example it could be something like user live system D system multi user target to determine the default system target equivalent to run level, the system target file defines the services that system D starts.

System allows you to manage various types of units on a system including services name service and target name target devices name device file system mount points name mount and sockets name socket now we’re going to do a comparison of system five, run levels and target units. I have briefly discussed system five before in the previous lecture. So run level zero is run level zero target and power off target in the description is shut down and power off the system in run level one. In system D run level one target and rescue target which is set up a rescue shell in run level two, three and four. The run level two, three, four target multiuser target and set up a non graphical multiuser shell.

Run level five is run level five target graphical target and this description is set up a graphical multiuser shell and six is run level six target or reboot target shut down and reboot the system. System d brings the system to the state defined by the system target performing system initialization tasks such as setting the host name initializing the network, initializing Se Linux based on its configuration printing a welcome banner initializing the system hardware based on kernel boot arguments mounting the file systems, including virtual file systems such as the Slash Proc file system cleaning up directories and slash VAR and starting swapping. Now let’s look at what is our default system D values or what is the current target unit. So we’re going to type in system Ctlget default it’s multiuser target. So this is by default. This is something I didn’t change myself. This is how the system was installed when I installed the sentos machine. So the multi user target indicates that the system is running in a multi user mode. This is similar to run level three in a system fight in its system. And you can also verify this in the old command run level. And as you can see, as soon as I run level it says run level is three. The default target units is represented by the Etsy CD two systemd.

CD two system. And then look for file called default target. So we get a cat on default target. So as you can see in this the description is equal to multi user system so we don’t have graphical device it will say graphical target instead of multiuser suppose if you want to change the default target then this is the command for that.

So as we know, we’re already using multiuser so we can type in systemctl set default and we can change it to since we are already on multi user, we can change it to graphical. Target and as you can see it removed the previous link, the symbolic link and created a new one which is pointing to graphical target. Okay? And if you want to check that doing an LS LRT etsy system default target we can see that our new target is pointing to the link graphical target instead of multiuser target. Okay? So now I’m going to since I’m not trying to run graphical user in this one, I am going to change it back. So systemctl set default, multiuser target. Multiuser target. And if up arrow and do an LS again it’s changed back to multiuser target.

 

  1. Scheduling tasks by using the cron utility

Hello and welcome again. In this lecture we’re going to discuss scheduling in Linux by using a utility called crontab. Crontab is very useful for routine tasks like scheduling, system scanning, daily backups, et cetera. Crontab executes job automatically in the background on a specified time interval. For scheduling one time tasks you can use at command on Linux if you do not have cron tab installing a system, you can use yum utility in cento s and Red hat to install it. Or you can use app get in ubuntu. This is what the syntax look like for our chron tab statement. And it has six fields. Field one through five denote the date time and the six fields are used for actual commands that you want to run. So going from your left, this is minute or what minute of the hour that you want to run the schedule job. This is for the hour, this is for the day of the month on a one through 31 day basis, month of the year and then day of the week, zero through six with Sunday being either zero or seven.

And then at the end you will put the actual command that you want to run at that specified time. And now we’re going to use some examples to actually show you what it looked like. So the application that needs to be installed on your system is called crony and you can just use an Rpmq like I have done here and space crony and it is showing that it installed on my system. How do you edit, add or edit a chron tab? So suppose I’m logged in as root. Right now the command that you use is chron tab e, and then we are just like in a text editor and whatever I type in here, it’s going to be my cron tab job. For now I’m going to exit it. And suppose if I want to have the cron job owned by another user, accept the root, then the way I run this is cron tab u username, let’s say larry e. Now this job is going to be owned by Larry.

Okay, let’s exit out up here as well. How do you list cron jobs? Suppose if you have several cron jobs listed on your machine, how do you find out how many are these and what are they? So right now I don’t own any. It says no cron job or no cron tapiruth. If I want to find out any kind of cron jobs that are listed by the user Larry, then I’ll run this command and so far he has nothing either. Okay, now let’s create I’m logged in as Larry right now and the way I switched it, I want to show you that also right now I’m logged in as root. I do an su switch user Larry and now I’m logged in as Larry and I’ll show you Larry. Okay, so the job that you’re going to create is let’s do this cron tab e. Then insert. I’m going to press I to insert. And here’s my cron job.

Zero 517, then three Srift star, star. And then I’m going to run a script. So this is my cron job. Let’s read it. So starting from left, we have zero. That means zero minutes of every hour. And then we have hour. We have, you know, for hour, we have 2 hours, five and 17. So five would be 05:00 A. m. , 17 would be 05:00 P. m. . So this job is going to run twice a day, 05:00 A. m. And 05:00 P. m. . And then for next three, I have Asterisk. That means it’s going to run every single day sorry, every day of the month, every month of the year and every day of the week. That’s what the three stress mean. Every day, every month and every year or every month of the year. Every day of the week and every day of the month. And then the actual command is, there’s a script residing in scriptscript sh. That’s the script that’s going to run on this specific time, twice a day, every day.

And then I’ll go ahead and skip out of it and save it. And now, if I were to let’s clear it. And if I want to look at Cron tab l. Now you can see Larry has one cron job. Before that, he had nothing. So now it’s showing up under his name. Let’s run one more job. And this job I’m going to run as root. So cron tab e insert zero, two star, star, star bin sh backup dot sh. So it’s going to be in this case, this is going to run at 02:00 A. m. Every day. So zero again, starting from left is zero is the minute, two is the hour and 02:00 A. m. . And then every day of the month, every month of the year and every day of the week, it’s going to run this script, which is called backup sh. So as you can see, this is a very good command for running a script. Instead of running it manually, you can just schedule it, but it has to be something that has to be run on a consistent basis. If it’s a one off kind of situation, then you might you’re probably better off just doing it a couple of times a year or whatever instead of having it scheduled like this.

 

  1. Shell Scripting

Hello and welcome again. In the last lecture we talked about cron jobs and that was a way to schedule a job. Now we are going to learn shell scripting at a very basic level, what shells are, and this is how to automate jobs. If you remember in the last lecture we talked about scripts which are run at specific time. Now we’re going to learn how to create those scripts so they can run on a specific time or they can run manually at will when you like to, and what shells basically do. These are short programs that are written in shell programming languages and the default programming language is bash. That is by default install on most Linux machines, most Unix machines as well. And they’re very easy to create scripts, as you can see in an example just in a minute. So in order to create my program, what I’m going to do is first I’m going to make a directory, I’m going to keep my scripts.

It’d be a good idea to keep it separately, right? So I’m going to call it my directory scripts. Now, if you look at it, I have a script directory. Now I’m going to CD into that directory if you do a PWD. Now I’m in that directory. Now I’m going to create my script. And in order to do that, I’m going to use bi editor and I’m going to call it Morning. Then I’m going to press insert. And the first line that goes into a script, it actually tells the interpreter where the script are, where the executable reside.

So in our case it is bin bash. This is where our script is. It’s always going to start with a hash sign, then a bang, and then the path to the executable, to the script. Suppose if you were using perl scripting instead of bash, then it’ll be user local bin or user local bin perl because that’s where mostly Pearl resides in most systems. Or use a bin perl, depending how you install Pearl and where it’s located. This is what the first line look like. Now the second line is going to be an actual command. You probably have seen me using Clear quite often. So I’m going to make my first executable statement. Anytime you have a pound sign or a hash sign at the beginning of a command, it doesn’t get executed. It gets executed as a comment. It’s just for the reader who’s reading the shell who might want to do some editing to the shell. It’s for them. It’s common information and it’s typical of all shell scripting and all programming languages as well, that the hash sign usually identifies a comment except in the first line where it’s telling you that the executable is in that line. 3

So that’s my first command. In the next command I want to that’s my second line echo and I want to say good morning, world encode. And when you have these double codes. Echo is going to print exactly what’s inside the double code as you’re going to see in a minute. Okay, so this is the first step. Our script is very short. It’s not doing a whole lot, it’s just doing these two commands. I’m going to do colon, WQ, bang. Okay, my script has been created. It’s cleared. This is my script. As you can see, it’s a regular file but look at the permission. Neither user nor the group nor anybody else has execute permission on this. So the first thing we have to do is chimed seven, let’s give it 744. So only the rest of the permissions are going to stay the same except for the creator or for the owner who’s Larry in this case. And morning. So I just changed the permissions. Now look at the permissions Larry has execute permission on the script.

The way you run a script is always started with A, and then I’m going to type in more and I’m going to use the tab to complete it. I just typed it mor and then I use tab to complete the name of the script. Enter and as you can see it says good morning world. First is cleared the screen, there was nothing to print there in the second command it was an echo command and it did a good morning world. That’s what we wrote inside the script. So this is how you write a script. This is not a scripting class. So I’m not going to go into detail because it’s a vast subject. There are actual books, I mean there are books out there talking exactly about how to create a script and there are very complex things that you can do, just like you can do it with any programming language. So shell is a programming language as well. So there’s a lot you can learn on it and you can be later on in your career become a scripter. Instead of doing everyday things on a Linux server you can become a shell scripter and then your job will be just to script if that’s what you enjoy. You like to do programming then this is definitely field that you can pick within Linux.

img