The cron Daemon (Demo)

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Time
21 hours 25 minutes
Difficulty
Intermediate
CEU/CPE
21
Video Transcription
00:00
>> Hey, Cybrarians, and welcome back to
00:00
the Linux Plus course here at Cybrary.
00:00
I'm your instructor, Rob Goelz.
00:00
In today's lesson, we're going to
00:00
be covering the cron Daemon.
00:00
Upon completion of today's lesson,
00:00
you're going to be able to understand
00:00
and realize the importance of
00:00
the cron Daemon and how it's used
00:00
>> in process scheduling.
00:00
>> We're going to talk about and understand
00:00
how the cron and anacron daemons work.
00:00
Then we're going to locate the files and
00:00
directories that cron and anacron use.
00:00
The cron daemon or daemon is what is
00:00
used in Linux to handle job scheduling.
00:00
That word cron comes from
00:00
the Greek word for time, chronos.
00:00
Now the cron daemon runs a job on a fixed schedule,
00:00
and jobs that we see running regularly are
00:00
generally foundational processes,
00:00
and they use cron schedules to
00:00
ensure that they run at regular intervals.
00:00
We're going to see this in
00:00
the demo at the end of the lesson.
00:00
Now the cron daemon, crond,
00:00
is configured to start up along with the system.
00:00
Crond runs in the background and
00:00
actually checks these things called user cron tables,
00:00
using utility crontab,
00:00
which we're going to discuss in the next lesson.
00:00
Crond also uses something called the etc crontab file,
00:00
which is the global system crontab.
00:00
That is used basically just to run
00:00
scripts at very specific intervals.
00:00
Those intervals are cron.hourly, daily, weekly.
00:00
Actually, these are directories,
00:00
etc/cron.hourly, cron.daily,
00:00
cron.weekly, cron.monthly,
00:00
are just places where you can place a task or a script.
00:00
Then when those times come up,
00:00
these things will be run at that specific time.
00:00
A system task script or any script can be placed
00:00
into these directories and will
00:00
run at those specified times,
00:00
and we'll see that in the demo.
00:00
Now the other thing to talk about here is anacron,
00:00
because in some Linux distributions alongside cron,
00:00
you also have this thing running called anacron.
00:00
You might be asking why.
00:00
Well, what happens if
00:00
a job is scheduled to run and the system is down?
00:00
This is a very good question,
00:00
and it really depends on what you're running.
00:00
If you're running cron, the job doesn't run.
00:00
The system's down.
00:00
If you're running anacron,
00:00
it will run the missed jobs when the system comes up,
00:00
because it's going to look at the timestamp on
00:00
the files in the directory in var/spool/anacron,
00:00
and also at the etc/anacrontab file
00:00
to determine when the job should have been run,
00:00
and when it should re-run the job if it was missed.
00:00
Note, the anacron is not
00:00
explicitly called out on the Linux Plus objectives,
00:00
but you're going to see it in real life.
00:00
I wanted to explain what it was.
00:00
Let's have a look at all of these files and directories
00:00
mentioned with some demo time.
00:00
Here we are in our demo environment,
00:00
and the first thing that we're going to do is take
00:00
a look at etc/crontab,
00:00
which is, again, that global default crontab file
00:00
that's used to schedule things.
00:00
This job definition example at the top is very helpful.
00:00
You can see the layout of how
00:00
this scheduling in cron is setup.
00:00
We're going to come back to that in
00:00
the next lesson in a lot more detail.
00:00
But just in general, we can see from left to right,
00:00
a job definition is set up
00:00
by the minute, then the hour,
00:00
then the day of month,
00:00
the month, and the day of the week.
00:00
We'll talk about that as we go through here.
00:00
The etc/crontab file, I said,
00:00
is just used to run the job scripts
00:00
contained in the specific interval directories.
00:00
The interval directories are things like cron.daily,
00:00
cron.weekly, cron.hourly,
00:00
and so on and so forth.
00:00
For instance, if we look at this
00:00
cron.hourly setting right here,
00:00
this schedule, it's set to run every 17 minutes
00:00
because this is 17 minutes and
00:00
the star indicates every time.
00:00
So 17 minutes of every hour,
00:00
every day of every month,
00:00
every month, and every day of every week.
00:00
If we go down and we look at cron.daily,
00:00
this is scheduled to run at 25 minutes after the hour,
00:00
at 6:00, which was 6:00 AM
00:00
>> because this is 24 hour time.
00:00
>> You can see right here, 0-23.
00:00
This is 6:25 AM, every day,
00:00
is when the cron.daily script
00:00
runs or anything in this cron.daily directory runs.
00:00
Then the same thing is true here of
00:00
cron weekly, and cron monthly.
00:00
Cron weekly runs on
00:00
Sunday because it says the seventh day is zero
00:00
or seven at 6:47 AM, and so on and so forth.
00:00
Let's go ahead and get out of there.
00:00
Before we leave this, you might be asking the question,
00:00
looking at this, why do we care?
00:00
You might need to adjust this.
00:00
If you're doing something,
00:00
or running something in this file and it
00:00
happens to bump into a backup window,
00:00
or another important process,
00:00
you may need to go in here and
00:00
change the scheduled time to
00:00
ensure that that doesn't bump
00:00
into somebody and cause issues.
00:00
Also inside of here,
00:00
we can see that these are all really using anacron.
00:00
Usr/sbin/anacron. Let's take a look at
00:00
the anacron file itself.
00:00
We can take a look at that by going to
00:00
less etc/anacron tab.
00:00
This file does a pretty terrible job
00:00
of explaining these columns.
00:00
But the columns here are the period,
00:00
the delay, the job identifier,
00:00
and then the command.
00:00
For instance, here we can see that the cron
00:00
daily has a recurrence of daily.
00:00
The period is one.
00:00
But we see cron weekly has recurrence of
00:00
seven because it's every seven days.
00:00
Then the delay here, this
00:00
is a really important thing here.
00:00
It tells you how long the delay is in
00:00
minutes that anacron will wait
00:00
before it sees a job it skipped.
00:00
If the system comes up and anacron says,
00:00
"Hey the timestamps are old, what should I do?"
00:00
It will look and see how many minutes
00:00
to wait till it runs the job.
00:00
Now let's take a look at some of the directories
00:00
that cron and anacron uses.
00:00
The next question, since we just talked about anacron,
00:00
is how does anacron know that a job has been missed?
00:00
Well, it looks at var/spool/anacron.
00:00
If we look in this directory, we see that
00:00
every file in here has a timestamp.
00:00
It's going to look at these timestamps,
00:00
and it's going to correspond against
00:00
the integral directories,
00:00
and see what stuff should be run and
00:00
>> if it has been run.
00:00
>> If it sees that it has been run here,
00:00
>> it'll kick it off.
00:00
>> Let's look at those interval directories.
00:00
If we do an ls on etc/cron, for instance,
00:00
cron.daily,
00:00
we can see in this file we have
00:00
a bunch of different scripts,
00:00
and they do just general foundational things.
00:00
We see this doing log rotate,
00:00
which it's going to rotate our logs that are
00:00
stored in the system to ensure that we don't run out
00:00
of disk space because we're writing too many logs.
00:00
We have the man-db command,
00:00
which is used to update the manual database files,
00:00
so that we always have accurate and
00:00
>> present manual files.
00:00
>> We're just seeing in general a lot of
00:00
the foundational processes in
00:00
Ubuntu are taking care of daily.
00:00
If we were to look at the weekly,
00:00
or monthly directories,
00:00
we really see that these just
00:00
basically do the same thing.
00:00
That's cron daily, cron hourly.
00:00
They don't really have much of anything in them.
00:00
Cron monthly, it really just has one setting.
00:00
Really most of what happens
00:00
in terms of this is done through the cron daily stuff.
00:00
I chose to show you this in Ubuntu because it
00:00
leverages a lot of cron's scheduling,
00:00
while Linux doesn't seem to have as much.
00:00
But with that, we've reached the end of this lesson.
00:00
In this lesson, we covered the importance of
00:00
the cron daemon in process scheduling
00:00
>> in Linux operation.
00:00
>> Then we talked about how the
00:00
cron and anacron daemons work.
00:00
Finally, we saw the files and directories
00:00
that are used by cron and anacron.
00:00
Thanks so much for being here and I look
00:00
forward to seeing you in the next lesson.
Up Next