In this lecture, we'll talk about Cron, which is one of the most useful utilities when it comes to 1
Linux administration. It allows you to schedule commands or tasks also known as cron jobs at a specific 2
time. 3
Cron runs as a daemon and is used to automate repetitive tasks like backups, monitoring disk space 4
usage, updating the system with the latest application versions, sending e-mails, rotating logs and 5
so on. 6
I think there is no Linux server that does not run at least a few cron jobs. 7
The word crontab has three different meanings: the text file that contains the user's cron jobs is 8
called the user's crontab. 9
There is such a file for each user and on Ubuntu the files are located in /var/spool/cron/crontabs. 10
Of course, only root can access the directory! And there’s a command called crontab used to manage the 11
crontab files. 12
This is the command. And there is also a configuration file for the system wide cron jobs called 13
crontab located in /etc 14
This one. 15
There are two types of cronjobs, system-wide and individual user cron jobs. Let's take them one by 16
one and start with the individual cron jobs. 17
Each user has a crontab file, which is short for cron table, where all scheduled cron jobs of that 18
user are specified. 19
These files are located in a subdirectory of /var/spool/cron/ 20
The exact location can depend on the Linux distribution. Being text files, you can edit the 21
cron tabfiles by hand using a text editor, even though they are not intended to be edited directly. 22
It's recommended to use the cron tab command instead. 23
This will open the file using your preferred or default text editor 24
and validate the file's structure on exit. 25
Let's go ahead and dive into the corn tab command. To display the contents of the crontab file of the 26
current user run crontab -l. At the beginning, the users have no crontabs. To edit 27
the current user’s crontab file run: crontab -e 28
E comes from edit. 29
You will be prompted with some editors to choose from if there is no default 30
editor set. I'm choosing vim. 31
Note that the lines that start with a hash sign are comments; each task to run has to be defined through 32
a single line that consists of 6 fields separated by a white space. 33
The first 5 fields define when the task will be run and the last field represents the command or the 34
script to run. The cron jobs can be scheduled to run by minute, hour, day of the month, month, 35
day of the week or any combination of this. 36
You don't have to memorize the time fields because they are described on the last line. 37
Minute hour, day of month, month, day of week and the command. 38
The first field indicates the minute. 39
The second of the hour. 40
Then comes the day of the month. 41
The month. 42
The day of the week and finally the command. 43
If for a specific field, you use an asterisk, a star, it means always or any value for that field, 44
for example, an asterisk in the day or the week field means every day or every week. 45
Note that the minimum time unit used by cron is the minute. 46
If you want to schedule a task to run every 15 seconds, then you cannot use cron. 47
Create a bash script with a white loop and run the script instead. Let's try some examples! Let's suppose 48
you want to run a backup script every day at six a.m. So the minute zero, the hour six a.m. and three 49
starts and the command for example /root/backup.sh 50
This is the path to a bash script. 51
I also have some remarks: the hour is given in 24 hour time format, so between zero and 23; do not 52
use p.m. or a.m. If you want to run the command at four p.m., you write 16 in the hour filled. 53
This is the hour field. 54
The day of the week, which is the last time field that specifies the time can be between zero and 55
seven, where 0 = 7 = Sunday. 56
So if I run the pick up script to run only on Sundays at 6 am I’ll write 0 or 7 in that field. 57
Like this! 58
The script will run on Sundays at 6 a.m. Let's see another example! Suppose you want to run a script 59
that monitors the free disk space every minute. 60
So every minute means star for the minute, star for the hour, 3 other starts and the path to the 61
script. 62
For example, check_space.sh. It will the run every minute. 63
Another example. 64
If you want to specify a list of discrete values for repetition, use the comma operator. For example 65
I want to run the task at 4, 6 and 10 a.m. so the minute will be zero. 66
And in the our field I write 4,6,10 67
Another one: if you want to specify a range of values 68
use the hyphen operator. 69
Let's say I want the task to run on weekdays between 9 and 5 p.m. once per hour, 70
so during working days and hours. 71
And that means: the minute 72
and now the time interval 9-10, it means between 9 and 17 or 5 p.m.. 73
Every day of month, every month, and in the last field, I write 1-5, it means from 74
Monday till Friday and the path to the command or to the script. 75
For example root, I don't know firewall.sh and another one, You can also use the slash operator to specify values 76
that will be repeated over a certain interval. 77
For example, if you have */3 78
In the day field, it means the action will be performed every three days. 79
Let's write such an example 20 4,21 */3 I have three fields so I need another 80
two. 81
And the path to the script; in this example, the task will be run every 3 days at 4 and 21 at 82
minute 10. 83
There are also several cron macros used to specify the most commonly used intervals. 84
Yearly or annually will run the specified command once a year on the 1st of January at midnight like 85
this: @yearly and the path to the script. 86
/root/happy_new_year 87
@monthly - Runs the specified task 88
once a month at midnight on the first day of the month. And @weekly - Runs the specified task once 89
a week at midnight on Sunday. 90
And @daily - Runs the specified task once a day at midnight. 91
And @hourly - Runs the specified task once an hour at the beginning of the hour. And finally 92
@reboot - Runs the specified task at boot-time. 93
Let's see a practical example! I want to append the current date and time to file every 2 minutes. 94
First, I'll comment out all the lines I've written; they were just examples and I'm writing * 95
It means every 2 minutes and then **** and now the command date >> /tmp/ 96
the path to a final date_and_time.txt And I'm saving the file and exiting. 97
So wq; to see that it really works, I'll display the cron log file in real time using tail-f so 98
tail -f /var/log/syslog this is the cront log file on Ubuntu; on Centoes is var/log/cron. 99
This is the name of the file on Centoes. 100
And we'll see shortly how cron runs the command. 101
I'm waiting for at the most two minutes. 102
And we see this line. 103
The cron task was executed. 104
I can also display the contents of the file that was created by Cron. 105
So cat/tmp 106
date_and_time.txt 107
And we'll see there the date. 108
To remove your crontab file run: crontab -r 109
I'm not removing it. 110
Note that root can edit, list or remove other users' crontAB t using the -u option and the username. 111
For example, I'm becoming root and create a cron tab for the student user. 112
so sudo 113
su I'm root 114
and crontab -e -u stud 115
And it opened the crontab of the user student and I'm adding something randomly. 116
I'm saving the cron job and then removing it. 117
So crontab -r -u stud 118
Now, the user student doesn't have any crontab. 119
An important remark is that cron sets and uses its own environmental variables. The path variable 120
is of special importance, because if the command you are executing is not present in the cron specified 121
path, you'll get an error. 122
And this is very common for beginners. 123
Use an obsolete path for that command or the script to execute. 124
Keep this in mind! 125
Root can also set some restrictions in regards to which users have access to their own crontab files: 126
man crontab 127
Take a look at this paragraph! 128
There are two files of importance: 129
cron.allow and /cron.deny These files will decide which users are allowed to use the crontab 130
command. 131
As you can see, scheduling cron jobs is easy. 132
However, there are a few web-based crontab syntax generators available to make this even easier. 133
Two such websites are crontab.guru/ and crontab-generator.org/ You can play around 134
to learn cron jobs examples. Just enter your desired dates and times and it will instantly generate 135
the crontab line for you. 136
At the end let me tell you a few words about system-wide cron jobs. Besides the user’s cron jobs 137
we've just seen, there are some system cron jobs that will be run as root. 138
How does it work? 139
You simply move your scripts inside some directories. 140
These are the directories /etc/cron.daily, hourly, monthly and weekly And the scripts will 141
be executed every hour, day, week or month. 142
For example, if I move my script to cron.hourly 143
the script will be executed hourly. 144
Then etc/crontab specifies when exactly will these cron jobs be run. 145
For example, all scripts inside the cron.weekly directory 146
will be run on Sunday at 6.47 a.m.. 147
Cron jobs are intended for servers or machines that are running continuously. If the system is down, 148
when the cron job is scheduled to run, the task is lost. 149
It will not be run when the machine starts. 150
In the next lecture, we'll talk about anacron, which can be used to execute commands periodically, 151
without assuming that the machine is running continuously.