- [Narrator] A recurring system task is not tied to a user
and is run by the operating system.
The format of a system cron job
is the same as a user cron job.
For more information on the cron job format,
please refer to the video in this course
on recurring user tasks.
Cron support is installed by default.
If cron is not installed on your machine,
use yum to install it by typing into a terminal
sudo space yum space install space dash y space cronie
space crontabs and hit Enter.
Type in your password and hit Enter again.
We also want to ensure the cron service is running.
Type in sudo space systemctl space start space crond
and hit Enter.
To ensure cron starts at boot,
type in sudo space systemctl space enable space crond
and hit Enter.
System cron jobs are stored in /etc/cron.d.
To create a cron job,
we'll need to create a new file inside this directory.
Since we're creating a system cron job,
we'll need to elevate privileges with sudo.
Type in sudo space vi space /etc/cron.d/backupdocs
and hit Enter.
Go into your insert mode by pressing the Insert key
and then type in 0 space 1 space * space *
space * space root space rsync space dash a
space /home/ your username,
I'll insert grant here, /Documents with a capital D/
the last slash is very important,
space /home/ your username /Documents.bak.
This cron job looks very similar to what we did
in the user cron job video with a couple of differences.
After the time specification,
we've included a user to run this cron job as.
In this case, the user is root.
We've also specified absolute paths
for the source and destination files
to ensure correct files get copied.
Any user can be specified to run system cron jobs.
If you use a normal user,
you will want to be sure
they can do the action you specified.
To test your command line,
log in as that user,
run the desired commands to make sure they work
and then put them in a cron tab file.
To save the cron job,
press Escape colon wq and hit Enter.
For system cron jobs, there are a few shortcuts.
If you have a script that you like to run every hour
and you don't want to fiddle with the cron tab,
just copy it to /etc/cron.hourly.
There are directories for hourly, daily,
weekly and monthly.
To see these directories,
type in ls space dash d space /etc/cron.* and hit Enter.
In most cases when we've edited
the configuration of a service,
we need to restart that service.
However, in the case of cron,
it reparses the cron tab files every minute
and loads them into memory
so there's no reason to restart the service
each time we change the files.
For more information, refer to the man pages for cron,
the cron tab command and the cron tab file.
The man page for cron covers the service itself
and we can view it by typing in man space cron
and hit Enter.
The man page for the cron tab command
explains the cron tab command's usage.
Press Q to quit
and then type in man space cron tab and hit Enter.
Perhaps the most useful man page
is the one for the cron tab file format.
To view this man page,
we have to specify the man page category.
Press Q to quit
and then type in man space 5 for the category
space cron tab and hit Enter.
The fifth man page category
is for file formats and specifications.
In this man page,
you'll find more information about the cron job file format.