In this lesson
we're going to discuss how you can schedule tasks
with Cron to help you automate
and perform tasks repetitively without much effort.
This means you'll learn how the programs can run
at specific times, such as every day at 2:00 AM,
and you don't even have to stay up late to run these things.
Now to do this, we're going to use Bash Scripting.
And Bash Scripting
is a powerful part of system administration
and development that can be used all the time.
It's going to be used by system administrators,
network administrators, developers,
scientists, and pretty much anyone who uses Linux
or Unix as their operating system.
These folks can all use Bash for system administration,
data crunching, web application deployment,
automated backups, creating custom scripts
for various pages, and other things like that.
By using Bash Scripting, we can contain a set of commands
for an appropriate runtime environment,
which is then going to be used to automate
the execution of whatever tasks we want.
However, there are plenty more methods
for automating tasks that you can perform every day as well,
and you don't even have to use Bash Scripting necessarily.
Now, one of the challenges
among the many advantages of being a system administrator
is that you have to run tasks
when you would rather be sleeping,
because most of our maintenance is done overnight.
For example, some tasks include regular recurring tasks
that you need to run overnight, like doing a backup,
and no one is going to be expected to be using
the computer resources during that overnight timeframe,
so it's a perfect time to get this maintenance done.
Instead of you having to drive to work
and be there every night at 3:00 AM,
you can just write a script and have it do it for you.
Now, personally,
I like to use two different service utilities
that will allow me to run different commands,
programs, and tasks at predetermined times.
These are the Cron and at services.
Now Cron and at are services
that enable system administrators to schedule tasks
to run at a specific time in the future.
The Cron service is used to do repetitive tasks,
like every Monday at 8:00 PM, whereas the at service
is going to provide a onetime task
that runs at a specific time.
Now the at command schedules
a command to run at a particular time
that you normally have permission to run it at.
So in other words, it's not going to be designed
for repetitive or regularly scheduled tasks.
Instead, we're going to use the at command
that can do something like sending a simple reminder message
or it can run a complex script.
Either way, you know that you're going to run it one time
and only one time, but at some time in the future.
You can start out by running the at command
at the command line and pass to it
the schedule time as your option.
Then it'll place you at a special prompt,
and at this prompt, you can type in the command
or the series of commands that you want to run
at a specific time.
When you're all done with that,
you'll press Control + D on a new line
and your command will be placed into the queue to run
at the time you had specified.
To run the at command, simply type in at,
the options, and the time that you want it to run at.
Some of the at command options include things like -m.
This is an option that's used to send mail to a user
when the job completes, regardless of the output.
The -M option will be used
if you want to prevent mail from being sent to the user.
Now, the -f and the file name option
is going to be used to read a job from a file
rather than from standard input.
And the -t and the time option is going to be used
to run the job at a specific time value,
whereas the -v option is used to display
the time the job will be executed.
And essentially it just means -v for view.
So you can see what things are in the queue to run later.
When you're using the at command,
there's a lot of different ways to specify the time.
For instance, there's a couple of keywords.
For instance, if you say noon,
that means 12:00 PM.
If you say tea time, that means 4:00 PM.
If you say midnight, that means 12:00 AM.
If I type now + 3 minutes,
that's going to specify I want the time right now,
plus three minutes for that action to happen.
Or now + 1 hour, would then specify the time
plus one hour from now.
If you use the atq command,
this is going to show you the queue
for all the tasks that are scheduled by the at command.
If you type atrm,
this will actually be used to delete a scheduled task.
It is basically the removal command.
Now, system administrators and users may want to have scripts
or commands that execute on a regular basis,
and at is not very helpful for that,
because at only schedules things one time.
Instead, if we want this regular type of automation,
like every Monday at midnight,
we would use something like Cron.
The Cron Daemon is going to be used to manage
these scheduled tasks called cron jobs.
And these cron jobs will happen every single time
at repeating interval.
The Cron Daemon will check its crontab configuration file
every single minute to discover whether there's any tasks
that need to be accomplished.
If there are, it's going to execute those.
If not, it goes back to sleep,
it waits a minute, and then it checks again.
The Cron Daemon is controlled using the crontab command.
This command assumes the current user,
unless you use the -u option to specify somebody else.
You can also create, view,
and delete crontab files using the crontab command.
Some of the options of the crontab command
include things like -e.
This is an option that's used to edit the crontab file
for the current user.
l is used to view the crontab file for the current user.
r is going to be used to delete the current crontab file.
And -u will be able to create a crontab file
on behalf of a specified user.
To run crontab, simply write crontab,
the options, and the file or user you want to specify.
Now, the crontab file are going to be referenced
by the Cron Daemon to determine
what scheduled tasks might exist.
The crontab file are going to be managed using
the crontab -e command, like we just talked about.
And this command will open up the Vim text editor by default
because this is the default editor on most Linux systems.
This will then allow a user to specify
when they want to run a task and what task they want to run.
Now, when you look at the crontab file,
you're going to see a list of every single scheduled task
on that Linux server.
This is going to be in the format of *****
and then the command.
For example, I might have something
that says 4523**6/home/user/scripts/exportdump.sh.
Now what this says is that 45 minutes after the 23rd hour
on the sixth day, I want to run this script.
Now, when you look at these columns,
the first column is always the minute,
the second is the hour, the third is the day,
the fourth is the month,
and the fifth is the day of the week.
So in this case, I said at 11:45 at night
on the sixth day of the week, which is Saturday,
you are going to do this export dump script.
Now in that script can be anything I want,
I can have something in there
that's going to dump the password credentials
on this server.
You can set these up however you want,
but the idea is that using crontab gives you
a way to schedule tasks inside of Linux.
For example, let's say there's a line that says,
*20**2-5/path/to/command
inside of my crontab file.
When it executes, it's going to do this
based on the time in this line.
The time here is 2000 hours, which translates to 8:00 PM,
and it's going to happen Monday through Friday,
which is days one through five of the week.
Now, if I had another line that said 152**/path/to/command,
this will execute at 2:15 AM every single day,
and then this tells me that because I have the second hour
and the 15 minutes in that line.
And then I'm doing it every day,
because that final thing that says day is actually a star.
Now, if I had another line that said,
3041**/path/2/command,
what do you think this is going to execute as?
Well, this is going to be 4:30 AM,
and then that third position tells me the day of the month.
And in this case, it's the first day of every month.
And so here you could see how we specify
these different crontab lines
to be able to specify the exact time
and the exact days that we want,
either by day of the month or days of the week.
The crontab file that contains
the scheduled task information
can be found in two directories.
The first is /etc/cron.d.
And the second is /var/spool/cron.
Both of these are directories.
Now your route user can also schedule systemwide tasks
by using the /etc/cron.d directory.
Services can also add scheduled tasks to this location too.
The root user can add tasks to the directory
or they can add tasks to the /etc/crontab file.
Regular users though are not allowed to populate
the /etc/cron directory.
So a standard user has to schedule their own tasks
in a personal directory.
And this is located at /var/spool/cron.
So you could see the difference here.
If it's a root task or a service task,
it's going to be in the /etc/cron.d directory.
If it's a specific user who's scheduling it,
it's going to be in /var/spool/cron.
Now any test listed here are going to execute
with the standard user credentials,
because it's only been done by the user.
If you're doing things in the /etc directory,
you're also going to have other default cron directories
that administrators can use to place scripts in
that'll be executed on a regular basis.
These directories include things
like /etc/cron.hourly,
/etc/cron.daily,
/etc/cron.weekly,
and /etc/cron.monthly.
As you can probably guess,
these are scripts are going to be executed either hourly,
daily, weekly, or monthly.
Now the link or copy of the script
inside of these directories will be used
to schedule your commands, based on those time frequencies,
and any system administrator can do it
as long as they have the right privileges
and permissions to those directories.