Hey, folks.
And welcome back.
And this video we're going to learn crontab, command, crontab, command is used to schedule tasks.
So if you are a system administrator and you run certain commands almost every day and for a specific
time, then you could actually go into the system, log in and run the command.
But it would be a little tedious to log in every day at the same time to do the same thing.
So why not use the crontab which will let you schedule your task or your job and run it as at a specified
timings?
So the usage of crontab is very simple.
You'll run crontab minus E to edit the crontab table.
Anything that goes inside of the crontab that is called table it because it is formatted in a way that
it has columns and rows.
Then you can run crontab minus L to list the crontab entries.
You could do crontab minus R to remove the crontab, and the cron d is actually the daemon or the service
that actually manages the scheduling.
And as you could actually stop and start or check the status of cron d by running our same command system
CTL status cron d and that is to manage the CRON deserves.
Now, when we go in to the crontab table by specifying the minus e option, you will have to enter your
entry in a specific way or format because that is how it is.
It is read by the crontab.
If you make a mistake, it will not execute your command for that specific time.
So the very first column in your crontab entry is for the minute and minute runs from zero to 59 and,
as you know, are 60 Minutes.
The second column is for our.
The third column is for day of the month.
Fourth column month.
One to twelve.
Fifth column as day of the week.
And the last column is where you specify the command that you wanted to schedule.
And by the way, many times when you are interviewing for a Linux job, this is one of the favorite
questions the interviewer would ask you tell me all the different columns in a crontab entry.
So definitely this is something you should remember each and every entry in.
What is it for?
All right.
So for this very purpose, what are we going to do is we're going to create a contest entry by scheduling
a task and scheduling a task and simply we will do Echo Command and that Echo Command is to output.
This is my first crontab entry and that output, I want to direct it to a file and the file name.
I would give it crontab dash entry.
All right.
So let's go into my Linux machine.
I have my Linux machine right here and logged in as myself, hostname as my first and next VM and the
path or the directory dead end is home.
I have so.
To you, the screen now, let's simply run the crontab minus E!
Now, before we run into crontab B, we need to know the scheduling timing that we y to specify a task
for this purpose.
I'm not going to specify something for tomorrow and future since I wanted to show it to you.
I will specify something that will happen in the next minute.
For me to do that, I would need to find out is it timing that is right now when I run the date command,
it gives me the time, the date and month.
So anyway, here is the hour and minutes.
So it is 16 hour, which is four pm and eight.
So I will specify the timing as six, twenty sixteen twenty.
So this would give me enough time to write the crontab entry as well.
So let's do crontab minus eight, so twenty one is the minute, sixteen is the hour and a day of the
month.
I wanted to, let's say, run every day month.
This is the month of October.
Let's see.
I just want to run it in October.
So 10 day of the week, every day, that's fine.
And now the command.
The command is echo.
This is my first crontab and tree.
OK, a line to output to a file called crontab entry.
Oh, by the way, if you notice as soon as you can crontab E., It actually gives you the via editing
mode.
Same way as if you are editing a file.
All right, so right, quit to save and get out.
Now let's do date.
It's 16, 20 and 21 seconds, so we have about 40 seconds until we see the result, whether our schedule
command ran as per the schedule.
So let's clear the screen.
Typekit date again.
Thirty nine seconds, 20 more seconds to go.
As soon as this command runs right here, it will create a crontab entry in my home directory because
I did not specify the full absolute path so that it will take the default path, which is my home directory
right here.
All right, let's do the date again.
All right.
Sixteen twenty one.
Now I should see that crontab entry in my directory.
So if I do it, that's minus L tr and there you go.
Bingo.
I see my crontab entry and let's see.
So not crontab entry.
I see the file that is created as part of my crontab entry.
If I Fireblade to read the file crontab entry.
You'll see this is my first crontab entry and this is exactly what I specified right here.
OK, so now if you wanted to see what is in your crontab entry, you could run Cron tab.
Minus L, and this will tell you the entry that is inside of the crontab table.
Now, if I wanted to remove everything from the crontab table, I would run the command crontab minus
R.
And now if I do crontab minus L, you will see there is no entry in my crontab.
All right.
Regarding the system, the the service, you could check the status of the current system, CTL status
Kranti and you will see it is up and running.
Now, if I stop the service, then I would still be able to create entries in the crontab.
However, it is not going to execute them because the service is not there or it's dead.
So that's how you schedule your future task or repetitive task using crontab.
I'll see you in the next lecture.