Now, we'll explore how to schedule tasks to be run at set times.
And Linux on servers, we sometimes need to set up some tasks to run automatically.
For example,
we could set up a job to automatically back up the database every Sunday at 3 a.m.
we have three main ways to set up such tasks
with the cron utility
with
Akron
or with a utility called
at. Now, here's the main purpose of each tool.
The cron utility is well suited for repetitive jobs that execute once
every few minutes or hours or on specific days or specific times.
Anacron is also used to create repetitive jobs, but with a few differences,
one is that it cannot repeat those jobs every few minutes or hours.
The smallest unit it can work with is a day.
So it can run a job every day or every three
days or every week or even every month or year,
but it cannot run a job multiple times per day just once per day.
This utility has been created because cron can miss jobs
if the computer is not powered on all the time.
For example, if a job is set to run daily at noon and the computer is powered on at 1201,
that job won't run for that day.
And Akron on the other hand, will check if that job for today ever got a chance to run.
If it didn't, it will execute it no matter when the system was powered on.
And now for the at utility,
we saw that cron and Anacron are focused on repetitive and automated tasks.
In contrast at is focused on tasks that should only run once.
Now,
the syntax of a cron job might be hard to remember
for the first few times you try to set one up,
but you can get a quick reminder by taking a look at this file.
This is Etsy Crontab.
This is the default systemwide cron table
and the commented lines tell you about the syntax you should use
if you ever need to add a cronjob here, add a new line at the end of the file.
For example, you could use a line like this.
The first five values tell cron when this should run.
The sixth value is a username
and this is followed by the command that should run.
Cron will execute this as if the user
specified in the username field launched that task.
The thing about adding jobs to this default
systemwide table is that in certain cases,
the package manager might overwrite this file
and we might lose the changes we've added.
For example, this could happen when the package manager upgrades cron.
So in our lesson, we'll learn how to add cron
jobs by editing a user's personal cron table instead of the systemwide table.
Just keep in mind when you're managing servers to first
look up cron jobs in the Etsy crontab systemwide table.
And afterwards look at the cron table belonging to the root user.
We'll soon learn how to do that.
But first,
let's dive into the first five values here
that establish when the cron job should run
in the first two fields, we pick the time
minute between zero and 59
hour, between zero and 23.
In the next two fields, we pick the day of the month,
between one and 31
and the month between one and 12.
And in the last field, we picked the day of the week between zero and 60 being Sunday,
one being Monday two being Tuesday and so on.
This can be a bit confusing since we may feel
it should naturally be a number between one and seven.
So cron accepts the alternate notation too with
one being Monday and seven being Sunday.
Instead of fixed numbers in these fields, we can also use alternate characters,
asterisk or star matches all values possible in that field.
So a star for the hour field means run it every hour, 012, all the way to 23.
The comma can be used to specify or enumerate multiple values for that field.
For example, if we want something to run at minute,
15 and minute 45 we could type in 15 comma 45 with no spaces in the first field.
And the dash can be used to specify a range.
For example, if we want to schedule a job to run at night
at hours 23 and 4 a.m. we could type this in the second field. 2-4
front slash specifies a step.
For example, if you have an asterisk in the hour field,
cron will step through hours one by one.
So by default, the step is one
first, the job will run at zero AM, then 1 a.m. then 2 a.m. and so on.
But if we want it to run every four hours, we need to change the step to four
to do this. We would type in
asterisk front slash four.
The job will now run at zero AM, then 4 a.m. then 8 a.m. 12 AM and so on.
We can also combine ranges with these steps.
For example, let's say we want to run something at only these three hours,
zero AM 4 a.m. and 8 a.m.
We could type in 0-8
front slash four in the hour field 0-8 specifying the range from zero AM to 8
a.m. and slash four telling cron to step through this range in four hour increments.
With theory alone, this kind of stuff can look a bit abstract and hard to digest.
So let's make it crystal clear by looking at some practical examples.
We'll add a simple cron
job that just creates a file.
This way, we can see if the file is created at a certain time.
And we'll know that our job executed correctly.
In cron
jobs, we can execute either commands or shell scripts
and we'll use the touch command here.
The first thing that's required is to find the full path to this command.
So again, we're doing this by typing in which touch.
It's important to remember that in Cronjobs,
we should always use the full path to a command.
We mentioned that we'll edit the user's cron table
instead of the system wide Chron table at Etsy Crontab
to edit a user Chron table. We can use the
E edit option in the Chron
tab command.
We'll type in Chron
tab.
E
this will edit the table of your current user. Whoever you're currently logged in as
to run the touch command and create a file called test underscore past
every day at 6:35 a.m. we would add this line.
You might notice that there's no field specifying a username.
Now as we had in the systemwide cron table.
This is not necessary here because this time cron sees who the
table belongs to and knows to run commands as that user.
Now,
we can save our file and exit the text
editor and that's all there is to adding a cronjob
to give you ideas about alternate time settings for a cronjob.
Here are some other lines that we could have used
to run this every Sunday at 3 a.m.
or we could use this notation
to run it at the middle of every month on the 15th at 3 a.m.
We would use this
to run it every day at 3 a.m. We use this
and to run it once at every hour when the minute is 00,
we'd use this.
If you want to practice and get used to writing cron expressions,
you can visit a website with the address www dot crontab dot guru.
To see the crontab of the current user use the
L list option
with crontab
L
to remove your user's crontab entirely. Use the
R remove option
to remove the cron table of a different user who
would type in this
an alternative way to set up cron jobs
is through the use of these special directories
daily,
hourly,
monthly and weekly.
Whatever cron finds in here, it will run daily, hourly, monthly or weekly.
Let's see how that works.
Now, imagine we have a shell script in our local directory.
Simply called shell script.
The shell script file should have no extension if we want to use it as a cron
job. For example, don't use a name such as shell script dot sh.
If you intend to use this with cron
to make this run hourly, we would follow this procedure
first copy the script to Etsy cron dot Hourly.
We need root privileges since only the root user can write to this directory.
Next, make sure the script is readable and executable. So cron can run it
and later on if you need to remove this job,
simply remove the file.
Now, let's move on to the next utility for scheduling jobs
on some
Ubuntu systems. Anacron might not be installed by default,
but we can install it with the command pseudo
apt install Anacron.
When using
Anacron, we don't really care what time the job will run.
We just want it to run daily monthly or once every few days
no matter what the time of day is.
So to schedule a job with Anacron, we would edit the Etsy Anacron tab file,
there are a few helpful hints in this file. First is that we can consult the Anacron
tab manual in section five to see more details about the syntax here.
So we can get some help with a command like man five
Anachro
tab.
Secondly, we already have three
Aron entries at the end of this file showing us some live examples.
And the syntax is quite simple. We have four fields in an
Aron tab file.
So this line makes the job run once every three days
with a delay of 10 minutes
and the job identifier or its name would be test job
and then the command
and the command in this job will run as the root user.
If we wanted to run it every week, we could write this.
And if we wanted to run every month,
we would write it this way.
Now, after we save our file, we should verify, if we use the correct syntax,
we can do that by passing the
T or test option to the Anacron command
to verify the syntax of our Anacron job is correct. We use the
T test option like this
and we'll get a message if there are errors.
But if it's correct, we'll get no message.
As mentioned in the intro,
cron and Anacron are meant to run jobs that repeat periodically.
And with the command, it's much easier to schedule jobs.
We just specify the time that we want the job to run.
For example, to have something run at 15 o'clock, we just type in at 15,
then we type the commands that we want to run at that time.
For example, here's our user bin touch file created by
at
and notice that we also use the full path to the command here.
Although in the case of at this is not an absolute requirement,
we could just type touch without proceeding that with the user bin path.
After each command, we want this job to run, we press enter
if we need to, we could add a second command and a third one and so on.
After pressing enter on the last command on the next empty line,
we would press control D to save this job.
We can also schedule this to run on a specific date
if we want to run it on a specific date. But also at a specific time, we can do that too.
And we can also use relative times and dates
to run this 30 minutes later,
we would use this
and to run it three hours later,
we could run this.
And as you may have guessed, we could do the same thing with days, weeks or months.
Note how we have to wrap our time and date specifications between single quotes.
Now to see what jobs are currently scheduled to run with at,
we can use the A TQ command or ATQ.
The first number in the output is a job id, which will be used in our next commands
later on. We might forget what a job was supposed to do.
So we can use the dash C or C
option,
which is like the cat command that prints the contents
of a file to display what an Atob contains.
Pretty cool. Right. We'll see a lot of output.
But the commands that we added to this job are included in the last lines
and to remove a scheduled out job,
we can use its job identifier at the end of the ATRM command.
For example,
ATRM one