The crontab Utility (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
and in today's lesson we're going to be
00:00
discussing the crontab Utility.
00:00
Upon completion of this lesson you are going to be able
00:00
to explain why crontab is used,
00:00
we're going to learn how the cron table is set up,
00:00
and then at the end, we're going to demo
00:00
creating some crontab entries.
00:00
As we mentioned in the previous lesson,
00:00
cron table files are just one of
00:00
the places that the cron daemon looks
00:00
to find scheduled jobs and
00:00
cron tables are stored in the user directory.
00:00
They can either be in /var/spool/cron/username
00:00
in CentOS or if you're talking about Ubuntu,
00:00
you're going to find that cron table for the user
00:00
in /var/spool/cron/crontabs and then the username.
00:00
You might be asking why use these directories
00:00
>> instead of the interval cron directories that
00:00
>> we talked about in the previous lesson?
00:00
Why not just use /etc/hourly and /etc/cron.daily?
00:00
Well, the cron tables in the crontab command are
00:00
used by users as well to schedule their jobs,
00:00
and they don't usually have
00:00
access to those /etc directories.
00:00
Sometimes they have jobs
00:00
>> that just don't fall into one of
00:00
>> those intervals that are used by
00:00
cron in those interval directories.
00:00
You should remember, if you're doing this in real life,
00:00
the cron job definition,
00:00
the job definition in the layout can always be
00:00
found in /etc/crontab in any distribution.
00:00
But for the purposes of the Linux plus exam,
00:00
the column meanings, the layout,
00:00
the fields, they are tested on,
00:00
and so you could use a mnemonic
00:00
if it helps you, it helps me.
00:00
I use "My Happy Day May Dawn" and that means minute,
00:00
hour, day of month,
00:00
month, and day of week.
00:00
Generally, in order to create
00:00
a cron entry or create a cron table,
00:00
user can use the command crontab -e,
00:00
which is e for edit,
00:00
and then the crontab is created by the user using
00:00
the job definition format
00:00
that we have over here on the right.
00:00
But let's go ahead and see this in action by
00:00
popping some tabs and having demo time.
00:00
Here we are in our CentOS system to do our demo today.
00:00
Like I said, when you're doing this in real life,
00:00
>> you can always look in /etc/crontab,
00:00
>> and then you can see this job definition.
00:00
I'm actually just going to cheat
00:00
a little bit and copy this,
00:00
and this is what I generally
00:00
suggest or recommend that people do as
00:00
well when they're trying to do
00:00
this on their own systems or in real life,
00:00
is just copy that definition and
00:00
then when you go into create a crontab,
00:00
which we can deal with crontab -e,
00:00
you can just hit ''Insert''
00:00
and then paste that guy in and you're good to go.
00:00
We're going to go ahead and just save
00:00
>> and close out of this
00:00
>> because let's think about what we want to do.
00:00
>> For the purpose of this cron in this cron table entry,
00:00
we'll just run on every minute an echo that
00:00
sends a message into
00:00
a temporary file or a file on our /tmp directory.
00:00
Let's first grab the absolute path to the echo command,
00:00
and remember we can do that by typing ''Which''
00:00
and then the name of the command we
00:00
want to know which is ''Echo.''
00:00
The reason that we need to do this,
00:00
and I'm just going to copy this as well,
00:00
the reason that we want to do this is because sometimes
00:00
cron doesn't have the same path as you or I do,
00:00
and so it doesn't necessarily know
00:00
where to find that echo command.
00:00
But if we copy this absolute path to it,
00:00
then cron will know where to find
00:00
echo to run the command that we wanted it run,
00:00
because it doesn't need to look for it,
00:00
it actually just knows absolute path to it.
00:00
Now that we know where echo is,
00:00
let's go ahead and create our cron table
00:00
or go back into our cron table rather,
00:00
with crontab -e,
00:00
and now what we can do is we can start
00:00
creating our entry.
00:00
I'm going to hit ''o'' to start on a new line,
00:00
and we're going to have this run on the every minute
00:00
of every hour of every day of every month,
00:00
in every day of every week.
00:00
The next thing we need to do is specify username.
00:00
But in this case, this is
00:00
my user crontab, I'm logged in as me,
00:00
so I don't need to specify the username,
00:00
it's just going to, by default,
00:00
run it as me.
00:00
What I'm going to have it do is run this echo command,
00:00
I'm going to have it run /usr/bin/echo,
00:00
and now what I'm going to actually have it
00:00
echo or print is
00:00
''This is running every minute'',
00:00
and then what I'll do is I will do a
00:00
double greater then sign to have
00:00
this append to the file /tmp/minutetest.
00:00
Then if I go ahead and hit ''Escape
00:00
: WQ,'' this will save this command,
00:00
and now what I can do,
00:00
is I can do a less on
00:00
/tmp/minutetest and
00:00
I can see that it is going to print the output.
00:00
This is running every minute,
00:00
and if we were to follow this,
00:00
if we were to do a tail -f on this file,
00:00
we would see that it's going to go ahead and enter
00:00
a new line into this file every time it
00:00
runs and it will keep
00:00
adding that to this file
00:00
because it appends as we go along.
00:00
But we can also see
00:00
the crontab entry for my user root by doing
00:00
an ls -al on /var/spool/cron/.
00:00
We're looking for the user rob,
00:00
not the user root, apologies.
00:00
If we look at this, we can
00:00
see that we don't have permissions,
00:00
we need to do it with sudo.
00:00
Let's go ahead and do sudo
00:00
less on /var/spool/cron/rob,
00:00
and now we can see the contents of my user file.
00:00
We can see that this has
00:00
the whole of the stuff that I configured
00:00
>> in my user cron.
00:00
>> This is helpful if you're trying to troubleshoot
00:00
a user's crontab, for instance.
00:00
The reason that we have that bsudo is because
00:00
only root can read other people's crontabs.
00:00
You have to either become the user as
00:00
a system administrator or you have to
00:00
go in as root to look at this.
00:00
But we can see the contents of
00:00
this file just by going in as
00:00
sudo or elevating privileges
00:00
to become root, to see this file.
00:00
But this is silly, so let's go ahead.
00:00
We can actually remove a crontab
00:00
entry and before we do that,
00:00
we can see that this is still running
00:00
every minute, so that's dumb.
00:00
We can remove a crontab by doing crontab -r,
00:00
which removes the crontab.
00:00
Now we can do a sudo ls -al on /var/spool/cron/,
00:00
and we'll see that there's no entry for
00:00
my user anymore, so it's been removed.
00:00
We've reached the end of this lesson.
00:00
In this lesson we covered why crontab is used in Linux,
00:00
and we talked about the layout and
00:00
the way a cron table is structured,
00:00
and then finally, we created
00:00
some cron table entries during the demo time.
00:00
Thanks so much for being here and I look
00:00
forward to seeing you in the next lesson.
Up Next