- [Instructor] At jobs are one times jobs
that run at a specific time.
Often we want to create recurring jobs
that run every minute, hour, day, week, or month.
In order to do this, we'll use the cron service.
A cron job is stored in a cron table or crontab.
There are two types of crontabs,
one for users and one for the system.
Let's talk about user crontabs.
User cron jobs are stored in the user crontab
and are specific to each user, so every user has one.
They can be managed by normal users,
there's no need to elevate privileges
for a user to manage their cron jobs.
They're stored /var/spool/cron/ the user name.
The other type of crontab is the system crontab.
System cron jobs are stored in the systemwide crontab
so they are run by the OS, not users.
Being that they're system wide,
the super user has to manage them.
System cron jobs are stored in /etc/cron.d.
No matter whether you're setting a user cron job,
or a system wide cron job, the format is the same.
The first column from the left
is minute of the hour to run the cron job.
In our case, it will run 45 minutes after the hour.
Valid values are zero through 59,
with zero being on the hour.
If there was an asterisk here, it would run every minute.
We can have more than one value in each field.
For instance, if we wanted the cron job
to run on the 15th, the 30th, and the 45th minute,
we'd enter 15, 30, 45 in this field.
We can do ranges as well.
We can also do step values by placing an */10
we're saying to run this every 10th minute,
skipping the nine in between.
To run something only on odd minutes,
we can combine a range and a step value.
This would run the cron job on every odd minute.
The range starts counting from one
and does every other minute.
The second column from the left is the hour of the day.
In our example, the cron job will run
only on the 23rd hour of the day, or 11pm.
Midnight is hour zero.
If there's an asterisk, the cron job will run every hour.
The third column is the day of the month.
Valid values are one through 31.
An asterisk here would mean that
it runs every day of the month.
The fourth column is the month of the year.
Valid values are one through 12,
or JAN to DEC, an asterisk will ensure it runs every month.
The fifth column is the day of the week.
Valid values are zero through six,
with zero being Sunday and six being Saturday.
We can also specify three letter
abbreviations for day names, such as SUN.
The sixth column is the command to run.
To help you create cron jobs,
there's an online cron job generator
at crontab-generator.org.
Cron support is installed by default.
If cron isn't installed on your machine,
use YUM to install it by typing into a terminal.
Type in sudo yum install -y cronie,
spelled C R O N I E, crontabs and hit enter.
Type in your password and hit enter again.
You will also want to ensure the service
is running and that it survives a reboot.
Type in sudo systemctl start crond and hit enter.
To make sure that cron is running on boot
type sudo systemctl enable crond and hit enter again.
Let's go ahead and type in clear to clear our screen.
To create a crontab type in crontab -e and hit enter.
This will bring up the user's crontab
in the default text editor, which will probably be VI.
Now let's use the same backup command line
that we used in the at service example.
Let's have it run the backup everyday at 1am.
Go into insert mode by pressing insert and then type in
0 1 * * * rsync -a ~/Documents/
~/Documents.bak and hit enter.
You might be tempted to place an asterisk after ~/Documents/
to copy all files, but don't.
The rsync command doesn't work quite
the same way as the cb command.
Placing the trailing slash is what you want
if you intend on copying the contents
of the document's directory only.
Save the file and exit by pressing
ESC + : WQ and hit enter.
To verify our user cron job type in crontab -l
which should show our cron job.
We can add multiple items to our user crontab
by just editing the file and adding more lines.
If we want to delete individual lines,
then edit the crontab file again with crontab -e.
To learn more about the crontab command,
check out the man page for crontab.
Type in man crontab and hit enter.
However, this doesn't cover the format of a crontab file.
To view this, we'll press Q to quit
and then type in man 5 crontab.
Category five is for file formats
and conventions and it's where
we'll find the specifics on the crontab format.