1
00:00:00,570 --> 00:00:01,589
In this lesson

2
00:00:01,589 --> 00:00:03,870
we're going to discuss how you can schedule tasks

3
00:00:03,870 --> 00:00:05,670
with Cron to help you automate

4
00:00:05,670 --> 00:00:08,970
and perform tasks repetitively without much effort.

5
00:00:08,970 --> 00:00:11,010
This means you'll learn how the programs can run

6
00:00:11,010 --> 00:00:13,980
at specific times, such as every day at 2:00 AM,

7
00:00:13,980 --> 00:00:16,770
and you don't even have to stay up late to run these things.

8
00:00:16,770 --> 00:00:19,350
Now to do this, we're going to use Bash Scripting.

9
00:00:19,350 --> 00:00:20,340
And Bash Scripting

10
00:00:20,340 --> 00:00:22,630
is a powerful part of system administration

11
00:00:23,553 --> 00:00:25,860
and development that can be used all the time.

12
00:00:25,860 --> 00:00:27,900
It's going to be used by system administrators,

13
00:00:27,900 --> 00:00:29,910
network administrators, developers,

14
00:00:29,910 --> 00:00:32,700
scientists, and pretty much anyone who uses Linux

15
00:00:32,700 --> 00:00:35,160
or Unix as their operating system.

16
00:00:35,160 --> 00:00:38,340
These folks can all use Bash for system administration,

17
00:00:38,340 --> 00:00:41,160
data crunching, web application deployment,

18
00:00:41,160 --> 00:00:43,620
automated backups, creating custom scripts

19
00:00:43,620 --> 00:00:46,410
for various pages, and other things like that.

20
00:00:46,410 --> 00:00:49,590
By using Bash Scripting, we can contain a set of commands

21
00:00:49,590 --> 00:00:51,540
for an appropriate runtime environment,

22
00:00:51,540 --> 00:00:52,920
which is then going to be used to automate

23
00:00:52,920 --> 00:00:55,410
the execution of whatever tasks we want.

24
00:00:55,410 --> 00:00:57,360
However, there are plenty more methods

25
00:00:57,360 --> 00:01:00,180
for automating tasks that you can perform every day as well,

26
00:01:00,180 --> 00:01:03,090
and you don't even have to use Bash Scripting necessarily.

27
00:01:03,090 --> 00:01:04,290
Now, one of the challenges

28
00:01:04,290 --> 00:01:07,230
among the many advantages of being a system administrator

29
00:01:07,230 --> 00:01:08,730
is that you have to run tasks

30
00:01:08,730 --> 00:01:10,110
when you would rather be sleeping,

31
00:01:10,110 --> 00:01:13,110
because most of our maintenance is done overnight.

32
00:01:13,110 --> 00:01:16,560
For example, some tasks include regular recurring tasks

33
00:01:16,560 --> 00:01:18,900
that you need to run overnight, like doing a backup,

34
00:01:18,900 --> 00:01:20,880
and no one is going to be expected to be using

35
00:01:20,880 --> 00:01:24,030
the computer resources during that overnight timeframe,

36
00:01:24,030 --> 00:01:26,910
so it's a perfect time to get this maintenance done.

37
00:01:26,910 --> 00:01:28,560
Instead of you having to drive to work

38
00:01:28,560 --> 00:01:30,990
and be there every night at 3:00 AM,

39
00:01:30,990 --> 00:01:33,990
you can just write a script and have it do it for you.

40
00:01:33,990 --> 00:01:35,010
Now, personally,

41
00:01:35,010 --> 00:01:37,380
I like to use two different service utilities

42
00:01:37,380 --> 00:01:39,390
that will allow me to run different commands,

43
00:01:39,390 --> 00:01:42,420
programs, and tasks at predetermined times.

44
00:01:42,420 --> 00:01:45,240
These are the Cron and at services.

45
00:01:45,240 --> 00:01:47,280
Now Cron and at are services

46
00:01:47,280 --> 00:01:49,740
that enable system administrators to schedule tasks

47
00:01:49,740 --> 00:01:52,710
to run at a specific time in the future.

48
00:01:52,710 --> 00:01:55,230
The Cron service is used to do repetitive tasks,

49
00:01:55,230 --> 00:01:58,620
like every Monday at 8:00 PM, whereas the at service

50
00:01:58,620 --> 00:02:00,540
is going to provide a onetime task

51
00:02:00,540 --> 00:02:02,640
that runs at a specific time.

52
00:02:02,640 --> 00:02:04,080
Now the at command schedules

53
00:02:04,080 --> 00:02:06,330
a command to run at a particular time

54
00:02:06,330 --> 00:02:08,490
that you normally have permission to run it at.

55
00:02:08,490 --> 00:02:10,440
So in other words, it's not going to be designed

56
00:02:10,440 --> 00:02:13,320
for repetitive or regularly scheduled tasks.

57
00:02:13,320 --> 00:02:15,810
Instead, we're going to use the at command

58
00:02:15,810 --> 00:02:18,450
that can do something like sending a simple reminder message

59
00:02:18,450 --> 00:02:20,910
or it can run a complex script.

60
00:02:20,910 --> 00:02:23,820
Either way, you know that you're going to run it one time

61
00:02:23,820 --> 00:02:27,090
and only one time, but at some time in the future.

62
00:02:27,090 --> 00:02:29,040
You can start out by running the at command

63
00:02:29,040 --> 00:02:30,930
at the command line and pass to it

64
00:02:30,930 --> 00:02:33,270
the schedule time as your option.

65
00:02:33,270 --> 00:02:35,940
Then it'll place you at a special prompt,

66
00:02:35,940 --> 00:02:38,310
and at this prompt, you can type in the command

67
00:02:38,310 --> 00:02:40,770
or the series of commands that you want to run

68
00:02:40,770 --> 00:02:42,420
at a specific time.

69
00:02:42,420 --> 00:02:43,830
When you're all done with that,

70
00:02:43,830 --> 00:02:46,260
you'll press Control + D on a new line

71
00:02:46,260 --> 00:02:49,020
and your command will be placed into the queue to run

72
00:02:49,020 --> 00:02:50,940
at the time you had specified.

73
00:02:50,940 --> 00:02:53,820
To run the at command, simply type in at,

74
00:02:53,820 --> 00:02:56,730
the options, and the time that you want it to run at.

75
00:02:56,730 --> 00:03:00,210
Some of the at command options include things like -m.

76
00:03:00,210 --> 00:03:03,120
This is an option that's used to send mail to a user

77
00:03:03,120 --> 00:03:06,796
when the job completes, regardless of the output.

78
00:03:06,796 --> 00:03:08,820
The -M option will be used

79
00:03:08,820 --> 00:03:11,610
if you want to prevent mail from being sent to the user.

80
00:03:11,610 --> 00:03:14,220
Now, the -f and the file name option

81
00:03:14,220 --> 00:03:16,590
is going to be used to read a job from a file

82
00:03:16,590 --> 00:03:18,510
rather than from standard input.

83
00:03:18,510 --> 00:03:21,630
And the -t and the time option is going to be used

84
00:03:21,630 --> 00:03:24,120
to run the job at a specific time value,

85
00:03:24,120 --> 00:03:26,580
whereas the -v option is used to display

86
00:03:26,580 --> 00:03:28,800
the time the job will be executed.

87
00:03:28,800 --> 00:03:31,620
And essentially it just means -v for view.

88
00:03:31,620 --> 00:03:34,560
So you can see what things are in the queue to run later.

89
00:03:34,560 --> 00:03:36,180
When you're using the at command,

90
00:03:36,180 --> 00:03:39,060
there's a lot of different ways to specify the time.

91
00:03:39,060 --> 00:03:41,160
For instance, there's a couple of keywords.

92
00:03:41,160 --> 00:03:42,810
For instance, if you say noon,

93
00:03:42,810 --> 00:03:44,310
that means 12:00 PM.

94
00:03:44,310 --> 00:03:47,100
If you say tea time, that means 4:00 PM.

95
00:03:47,100 --> 00:03:50,040
If you say midnight, that means 12:00 AM.

96
00:03:50,040 --> 00:03:52,230
If I type now + 3 minutes,

97
00:03:52,230 --> 00:03:54,690
that's going to specify I want the time right now,

98
00:03:54,690 --> 00:03:57,150
plus three minutes for that action to happen.

99
00:03:57,150 --> 00:04:00,630
Or now + 1 hour, would then specify the time

100
00:04:00,630 --> 00:04:02,730
plus one hour from now.

101
00:04:02,730 --> 00:04:04,890
If you use the atq command,

102
00:04:04,890 --> 00:04:06,360
this is going to show you the queue

103
00:04:06,360 --> 00:04:09,420
for all the tasks that are scheduled by the at command.

104
00:04:09,420 --> 00:04:11,280
If you type atrm,

105
00:04:11,280 --> 00:04:13,980
this will actually be used to delete a scheduled task.

106
00:04:13,980 --> 00:04:16,410
It is basically the removal command.

107
00:04:16,410 --> 00:04:19,500
Now, system administrators and users may want to have scripts

108
00:04:19,500 --> 00:04:22,200
or commands that execute on a regular basis,

109
00:04:22,200 --> 00:04:24,180
and at is not very helpful for that,

110
00:04:24,180 --> 00:04:27,150
because at only schedules things one time.

111
00:04:27,150 --> 00:04:30,210
Instead, if we want this regular type of automation,

112
00:04:30,210 --> 00:04:32,340
like every Monday at midnight,

113
00:04:32,340 --> 00:04:34,380
we would use something like Cron.

114
00:04:34,380 --> 00:04:36,480
The Cron Daemon is going to be used to manage

115
00:04:37,420 --> 00:04:39,180
these scheduled tasks called cron jobs.

116
00:04:39,180 --> 00:04:41,940
And these cron jobs will happen every single time

117
00:04:41,940 --> 00:04:43,590
at repeating interval.

118
00:04:43,590 --> 00:04:47,100
The Cron Daemon will check its crontab configuration file

119
00:04:47,100 --> 00:04:50,070
every single minute to discover whether there's any tasks

120
00:04:50,070 --> 00:04:51,480
that need to be accomplished.

121
00:04:51,480 --> 00:04:53,640
If there are, it's going to execute those.

122
00:04:53,640 --> 00:04:55,530
If not, it goes back to sleep,

123
00:04:55,530 --> 00:04:58,050
it waits a minute, and then it checks again.

124
00:04:58,050 --> 00:05:01,680
The Cron Daemon is controlled using the crontab command.

125
00:05:01,680 --> 00:05:03,750
This command assumes the current user,

126
00:05:03,750 --> 00:05:07,650
unless you use the -u option to specify somebody else.

127
00:05:07,650 --> 00:05:09,210
You can also create, view,

128
00:05:09,210 --> 00:05:12,870
and delete crontab files using the crontab command.

129
00:05:12,870 --> 00:05:14,910
Some of the options of the crontab command

130
00:05:14,910 --> 00:05:16,830
include things like -e.

131
00:05:16,830 --> 00:05:19,710
This is an option that's used to edit the crontab file

132
00:05:19,710 --> 00:05:21,600
for the current user.

133
00:05:21,600 --> 00:05:25,770
l is used to view the crontab file for the current user.

134
00:05:25,770 --> 00:05:29,220
r is going to be used to delete the current crontab file.

135
00:05:29,220 --> 00:05:32,130
And -u will be able to create a crontab file

136
00:05:32,130 --> 00:05:34,500
on behalf of a specified user.

137
00:05:34,500 --> 00:05:37,290
To run crontab, simply write crontab,

138
00:05:37,290 --> 00:05:40,740
the options, and the file or user you want to specify.

139
00:05:40,740 --> 00:05:43,050
Now, the crontab file are going to be referenced

140
00:05:43,050 --> 00:05:44,850
by the Cron Daemon to determine

141
00:05:44,850 --> 00:05:47,370
what scheduled tasks might exist.

142
00:05:47,370 --> 00:05:49,650
The crontab file are going to be managed using

143
00:05:49,650 --> 00:05:52,620
the crontab -e command, like we just talked about.

144
00:05:52,620 --> 00:05:57,000
And this command will open up the Vim text editor by default

145
00:05:57,000 --> 00:06:00,300
because this is the default editor on most Linux systems.

146
00:06:00,300 --> 00:06:02,370
This will then allow a user to specify

147
00:06:02,370 --> 00:06:06,300
when they want to run a task and what task they want to run.

148
00:06:06,300 --> 00:06:08,370
Now, when you look at the crontab file,

149
00:06:08,370 --> 00:06:10,860
you're going to see a list of every single scheduled task

150
00:06:10,860 --> 00:06:12,540
on that Linux server.

151
00:06:12,540 --> 00:06:14,790
This is going to be in the format of *****

152
00:06:18,396 --> 00:06:19,590
and then the command.

153
00:06:19,590 --> 00:06:21,480
For example, I might have something

154
00:06:21,480 --> 00:06:26,480
that says 4523**6/home/user/scripts/exportdump.sh.

155
00:06:33,210 --> 00:06:37,230
Now what this says is that 45 minutes after the 23rd hour

156
00:06:37,230 --> 00:06:40,800
on the sixth day, I want to run this script.

157
00:06:40,800 --> 00:06:42,270
Now, when you look at these columns,

158
00:06:42,270 --> 00:06:44,010
the first column is always the minute,

159
00:06:44,010 --> 00:06:46,950
the second is the hour, the third is the day,

160
00:06:46,950 --> 00:06:48,360
the fourth is the month,

161
00:06:48,360 --> 00:06:50,430
and the fifth is the day of the week.

162
00:06:50,430 --> 00:06:54,060
So in this case, I said at 11:45 at night

163
00:06:54,060 --> 00:06:56,490
on the sixth day of the week, which is Saturday,

164
00:06:56,490 --> 00:06:59,340
you are going to do this export dump script.

165
00:06:59,340 --> 00:07:02,010
Now in that script can be anything I want,

166
00:07:02,010 --> 00:07:03,120
I can have something in there

167
00:07:03,120 --> 00:07:04,860
that's going to dump the password credentials

168
00:07:04,860 --> 00:07:05,910
on this server.

169
00:07:05,910 --> 00:07:07,650
You can set these up however you want,

170
00:07:07,650 --> 00:07:10,200
but the idea is that using crontab gives you

171
00:07:10,200 --> 00:07:13,110
a way to schedule tasks inside of Linux.

172
00:07:13,110 --> 00:07:15,667
For example, let's say there's a line that says,

173
00:07:15,667 --> 00:07:20,667
*20**2-5/path/to/command

174
00:07:23,130 --> 00:07:25,350
inside of my crontab file.

175
00:07:25,350 --> 00:07:27,660
When it executes, it's going to do this

176
00:07:27,660 --> 00:07:29,700
based on the time in this line.

177
00:07:29,700 --> 00:07:33,900
The time here is 2000 hours, which translates to 8:00 PM,

178
00:07:33,900 --> 00:07:36,090
and it's going to happen Monday through Friday,

179
00:07:36,090 --> 00:07:38,700
which is days one through five of the week.

180
00:07:38,700 --> 00:07:43,700
Now, if I had another line that said 152**/path/to/command,

181
00:07:46,320 --> 00:07:50,760
this will execute at 2:15 AM every single day,

182
00:07:50,760 --> 00:07:53,520
and then this tells me that because I have the second hour

183
00:07:53,520 --> 00:07:56,280
and the 15 minutes in that line.

184
00:07:56,280 --> 00:07:57,840
And then I'm doing it every day,

185
00:07:57,840 --> 00:08:01,560
because that final thing that says day is actually a star.

186
00:08:01,560 --> 00:08:03,450
Now, if I had another line that said,

187
00:08:03,450 --> 00:08:08,370
3041**/path/2/command,

188
00:08:10,200 --> 00:08:12,660
what do you think this is going to execute as?

189
00:08:12,660 --> 00:08:15,330
Well, this is going to be 4:30 AM,

190
00:08:15,330 --> 00:08:18,630
and then that third position tells me the day of the month.

191
00:08:18,630 --> 00:08:21,690
And in this case, it's the first day of every month.

192
00:08:21,690 --> 00:08:23,340
And so here you could see how we specify

193
00:08:23,340 --> 00:08:25,080
these different crontab lines

194
00:08:25,080 --> 00:08:26,700
to be able to specify the exact time

195
00:08:26,700 --> 00:08:28,350
and the exact days that we want,

196
00:08:28,350 --> 00:08:31,620
either by day of the month or days of the week.

197
00:08:31,620 --> 00:08:33,390
The crontab file that contains

198
00:08:33,390 --> 00:08:35,070
the scheduled task information

199
00:08:35,070 --> 00:08:36,659
can be found in two directories.

200
00:08:36,659 --> 00:08:40,077
The first is /etc/cron.d.

201
00:08:40,077 --> 00:08:44,370
And the second is /var/spool/cron.

202
00:08:44,370 --> 00:08:46,200
Both of these are directories.

203
00:08:46,200 --> 00:08:49,590
Now your route user can also schedule systemwide tasks

204
00:08:49,590 --> 00:08:53,460
by using the /etc/cron.d directory.

205
00:08:53,460 --> 00:08:57,390
Services can also add scheduled tasks to this location too.

206
00:08:57,390 --> 00:08:59,700
The root user can add tasks to the directory

207
00:08:59,700 --> 00:09:04,260
or they can add tasks to the /etc/crontab file.

208
00:09:04,260 --> 00:09:06,960
Regular users though are not allowed to populate

209
00:09:06,960 --> 00:09:09,690
the /etc/cron directory.

210
00:09:09,690 --> 00:09:13,140
So a standard user has to schedule their own tasks

211
00:09:13,140 --> 00:09:14,520
in a personal directory.

212
00:09:14,520 --> 00:09:19,050
And this is located at /var/spool/cron.

213
00:09:19,050 --> 00:09:20,610
So you could see the difference here.

214
00:09:20,610 --> 00:09:23,010
If it's a root task or a service task,

215
00:09:23,010 --> 00:09:26,850
it's going to be in the /etc/cron.d directory.

216
00:09:26,850 --> 00:09:29,040
If it's a specific user who's scheduling it,

217
00:09:29,040 --> 00:09:33,360
it's going to be in /var/spool/cron.

218
00:09:33,360 --> 00:09:35,670
Now any test listed here are going to execute

219
00:09:35,670 --> 00:09:37,590
with the standard user credentials,

220
00:09:37,590 --> 00:09:40,800
because it's only been done by the user.

221
00:09:40,800 --> 00:09:43,590
If you're doing things in the /etc directory,

222
00:09:43,590 --> 00:09:46,320
you're also going to have other default cron directories

223
00:09:46,320 --> 00:09:48,480
that administrators can use to place scripts in

224
00:09:48,480 --> 00:09:50,940
that'll be executed on a regular basis.

225
00:09:50,940 --> 00:09:52,530
These directories include things

226
00:09:52,530 --> 00:09:56,293
like /etc/cron.hourly,

227
00:09:56,293 --> 00:09:58,863
/etc/cron.daily,

228
00:09:58,863 --> 00:10:02,250
/etc/cron.weekly,

229
00:10:02,250 --> 00:10:05,600
and /etc/cron.monthly.

230
00:10:05,600 --> 00:10:07,050
As you can probably guess,

231
00:10:07,050 --> 00:10:09,690
these are scripts are going to be executed either hourly,

232
00:10:09,690 --> 00:10:12,300
daily, weekly, or monthly.

233
00:10:12,300 --> 00:10:14,010
Now the link or copy of the script

234
00:10:14,010 --> 00:10:16,380
inside of these directories will be used

235
00:10:16,380 --> 00:10:19,290
to schedule your commands, based on those time frequencies,

236
00:10:19,290 --> 00:10:21,240
and any system administrator can do it

237
00:10:21,240 --> 00:10:23,280
as long as they have the right privileges

238
00:10:23,280 --> 00:10:25,130
and permissions to those directories.

