Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1

given directory (in this case, /etc/cron.hourly). So, in this case, the job
executes at 00:17 (17 minutes past midnight), 01:17, 02:17, 03:17, and so on
and uses all the programs listed in the cron.hourly directory.


The next job runs at minute 25 and hour 6 of every day of every month,
running run-parts /etc/cron.daily. Because of the hour limitation,
this script runs only once per day, at 6:25 a.m. Note that it uses minute 25
rather than minute 17 so that daily jobs do not clash with hourly jobs. You
should be able to guess what the next two jobs do simply by looking at the
commands they run.


Each of those four directories (cron.hourly, cron.daily,
cron.weekly, and cron.monthly) contains a collection of shell scripts
that are run by run-parts. For example, in cron.daily you have scripts
like logrotate, which handles backing up of log files, and makewhatis,
which updates the whatis database. You can add other system tasks to these
directories if you want to, but be careful to ensure that your scripts are
correct.


CAUTION
The cron daemon reads all the system crontab files and all user
crontab files once a minute (on the minute; that is, at 6:00:00, 6:01:00,
and so on) to check for changes. However, any new jobs it finds will not be
executed until at least 1 minute has passed.
For example, if it is 6:01:49 (that is, 49 seconds past 1 minute past 6:00
a.m.) and you set a cron job to run at 6:02, it does not execute. At 6:02,
the cron daemon rereads its configuration files and sees the new job, but it
is not able to execute it. If you set the job to run at 6:02 a.m. every day, it is
executed the following morning and every subsequent morning.
This same situation exists when deleting jobs. If it is 6:01:49 and you have
a job scheduled to run at 6:02, deleting it makes no difference: cron runs it
before it rereads the crontab files for changes. However, after it has
reread the crontab file and noticed that the job is no longer there, it will
not be executed in subsequent days.

There are alternative ways of specifying dates. For example, you can use sets
of dates and times by using hyphens or commas; for example, hours 9–15
would execute at 9, 10, 11, 12, 13, 14, and 15 (from 9:00 a.m. to 3:00 p.m.),
whereas 9, 11, 13, 15 would skip the even hours. Note that it is important that
you not put spaces into these sets because the cron daemon interprets a
space as the next field. You can define a step value with a slash (/) to show

Free download pdf