WEBVTT

1
00:00:00.260 --> 00:00:02.910
<v Instructor>In this lesson, we will learn about Cron</v>

2
00:00:02.910 --> 00:00:04.830
and Scheduled Tasks.

3
00:00:04.830 --> 00:00:08.130
Cron and Scheduled Tasks are tools used

4
00:00:08.130 --> 00:00:10.710
to automate the execution of scripts

5
00:00:10.710 --> 00:00:14.010
and commands at predefined times in Linux

6
00:00:14.010 --> 00:00:16.980
and Windows operating systems respectively.

7
00:00:16.980 --> 00:00:19.140
Cron is short for Chronos,

8
00:00:19.140 --> 00:00:20.820
the Greek word for time.

9
00:00:20.820 --> 00:00:24.540
The Cron application is a time-based job scheduler

10
00:00:24.540 --> 00:00:26.100
in Linux systems.

11
00:00:26.100 --> 00:00:30.030
Cron allows users to automate repetitive tasks

12
00:00:30.030 --> 00:00:34.560
like running security scans or updating system logs

13
00:00:34.560 --> 00:00:39.210
by specifying the exact time and frequency of execution.

14
00:00:39.210 --> 00:00:43.740
Scheduled tasks in Windows serve a similar purpose,

15
00:00:43.740 --> 00:00:46.440
enabling the automation of tasks

16
00:00:46.440 --> 00:00:48.720
such as running PowerShell scripts

17
00:00:48.720 --> 00:00:52.320
or performing system maintenance at scheduled intervals.

18
00:00:52.320 --> 00:00:55.680
Let's learn more about Cron and Scheduled Tasks

19
00:00:55.680 --> 00:00:58.560
then we'll conduct Cron demonstration.

20
00:00:58.560 --> 00:01:00.330
First we have Cron.

21
00:01:00.330 --> 00:01:04.860
Cron is a time-based job scheduler used in Linux systems

22
00:01:04.860 --> 00:01:08.970
to automate tasks by specifying the exact time

23
00:01:08.970 --> 00:01:11.220
and frequency of execution.

24
00:01:11.220 --> 00:01:12.810
It is particularly useful

25
00:01:12.810 --> 00:01:17.160
for automating repetitive tasks like running security scans

26
00:01:17.160 --> 00:01:21.000
or updating system logs at predefined intervals.

27
00:01:21.000 --> 00:01:24.690
Cron is controlled by a file called the crontab

28
00:01:24.690 --> 00:01:28.140
where users define tasks they want to schedule.

29
00:01:28.140 --> 00:01:32.370
Each line in the crontab file represents a task

30
00:01:32.370 --> 00:01:36.570
specifying the minute, hour, day of the month, month

31
00:01:36.570 --> 00:01:39.540
and day of the week when the task should run.

32
00:01:39.540 --> 00:01:41.700
Through scheduling and automation,

33
00:01:41.700 --> 00:01:44.820
Cron ensures that tasks are executed

34
00:01:44.820 --> 00:01:48.810
without manual intervention, making it an important tool

35
00:01:48.810 --> 00:01:52.050
for maintaining automated system operations.

36
00:01:52.050 --> 00:01:56.400
Specifically the crontab file follows a specific format

37
00:01:56.400 --> 00:01:59.220
to determine when tasks are executed.

38
00:01:59.220 --> 00:02:02.910
For example, a line in the crontab might read,

39
00:02:02.910 --> 00:02:07.910
20 20 * * 1-5 and then the name of the script,

40
00:02:09.140 --> 00:02:12.930
/scripts/backup-server.

41
00:02:12.930 --> 00:02:16.980
This would schedule a script to run every day defined

42
00:02:16.980 --> 00:02:20.760
as days one through five in the crontab format,

43
00:02:20.760 --> 00:02:25.710
and it would execute at 8:20 PM or the 20th hour

44
00:02:25.710 --> 00:02:27.210
and the 20th minute.

45
00:02:27.210 --> 00:02:30.690
The flexibility of crontab format allows users

46
00:02:30.690 --> 00:02:33.930
to define schedules down to the minute

47
00:02:33.930 --> 00:02:37.290
or for specific days of the week or month.

48
00:02:37.290 --> 00:02:40.260
This scheduling capability is crucial

49
00:02:40.260 --> 00:02:44.250
for security operations where tasks like integrity checks

50
00:02:44.250 --> 00:02:47.400
or log analysis need to occur regularly

51
00:02:47.400 --> 00:02:50.820
to ensure the system remains secure and stable.

52
00:02:50.820 --> 00:02:54.330
Finally, Cron can be integrated with bash scripts

53
00:02:54.330 --> 00:02:58.710
to perform various tasks such as a scanning log files,

54
00:02:58.710 --> 00:03:02.910
monitoring system activity, or checking file integrity.

55
00:03:02.910 --> 00:03:05.640
For instance, a script could be set up

56
00:03:05.640 --> 00:03:07.830
to compare the current hash values

57
00:03:07.830 --> 00:03:11.280
of files against the known good hashes

58
00:03:11.280 --> 00:03:14.340
to detect unauthorized modifications.

59
00:03:14.340 --> 00:03:16.830
If the values do not match,

60
00:03:16.830 --> 00:03:18.930
the script could trigger an alert,

61
00:03:18.930 --> 00:03:21.180
notifying system administrators

62
00:03:21.180 --> 00:03:23.910
of potential security breaches.

63
00:03:23.910 --> 00:03:27.840
This capability allows for continuous monitoring

64
00:03:27.840 --> 00:03:32.370
and automated responses to potential security threats,

65
00:03:32.370 --> 00:03:35.250
reducing the need for manual oversight.

66
00:03:35.250 --> 00:03:39.540
So overall, Cron is used in automating system maintenance

67
00:03:39.540 --> 00:03:43.020
and security operations on Linux systems.

68
00:03:43.020 --> 00:03:46.920
By scheduling tasks to run at specific intervals,

69
00:03:46.920 --> 00:03:49.110
administrators can ensure

70
00:03:49.110 --> 00:03:53.520
that important processes like system updates, backups,

71
00:03:53.520 --> 00:03:58.520
and scans are executed consistently and on time.

72
00:03:58.680 --> 00:04:02.550
This automation reduces the risk of human error

73
00:04:02.550 --> 00:04:05.490
and ensures that the system remains secure

74
00:04:05.490 --> 00:04:10.440
and well maintained without constant manual intervention.

75
00:04:10.440 --> 00:04:13.470
Second, we have Scheduled Tasks.

76
00:04:13.470 --> 00:04:17.340
Scheduled Tasks is a time-based job scheduler

77
00:04:17.340 --> 00:04:21.240
used in Windows systems to automate tasks

78
00:04:21.240 --> 00:04:26.220
by specifying the exact time and frequency of execution.

79
00:04:26.220 --> 00:04:27.870
It is particularly useful

80
00:04:27.870 --> 00:04:30.300
for automating repetitive tasks

81
00:04:30.300 --> 00:04:34.170
like running maintenance scripts, performing system backups,

82
00:04:34.170 --> 00:04:37.890
or deploying software updates at set intervals.

83
00:04:37.890 --> 00:04:39.660
Scheduled Tasks are managed

84
00:04:39.660 --> 00:04:41.850
through the Windows Task scheduler

85
00:04:41.850 --> 00:04:45.210
where users define and configure tasks.

86
00:04:45.210 --> 00:04:50.100
Each task can be set to run at a specific time on a daily,

87
00:04:50.100 --> 00:04:53.460
weekly, or monthly basis without the need

88
00:04:53.460 --> 00:04:55.320
for manual intervention.

89
00:04:55.320 --> 00:04:58.770
Scheduled tasks follow a straightforward process

90
00:04:58.770 --> 00:05:03.030
for setting up when and how tasks are executed.

91
00:05:03.030 --> 00:05:06.750
For instance, users can specify a task

92
00:05:06.750 --> 00:05:09.690
to run every day at a certain time

93
00:05:09.690 --> 00:05:13.650
or every week on a particular day.

94
00:05:13.650 --> 00:05:17.760
This flexibility is important for system administrators

95
00:05:17.760 --> 00:05:21.870
who need to schedule things like automated security scans

96
00:05:21.870 --> 00:05:24.150
or regular system checks

97
00:05:24.150 --> 00:05:27.030
to keep the system running smoothly.

98
00:05:27.030 --> 00:05:30.600
Scheduling tasks in advance helps ensure

99
00:05:30.600 --> 00:05:34.620
that critical operations are carried out on time,

100
00:05:34.620 --> 00:05:38.460
improving system stability and security.

101
00:05:38.460 --> 00:05:41.010
Additionally, Scheduled Tasks can work

102
00:05:41.010 --> 00:05:46.010
alongside PowerShell scripts to automate a variety of tasks.

103
00:05:46.020 --> 00:05:49.680
For example, a PowerShell script could be scheduled

104
00:05:49.680 --> 00:05:54.090
to scan logs for errors, send alerts for potential issues,

105
00:05:54.090 --> 00:05:57.660
or automatically back up important files.

106
00:05:57.660 --> 00:06:02.220
This integration allows for powerful automated workflows

107
00:06:02.220 --> 00:06:06.210
that help administrators keep the system running efficiently

108
00:06:06.210 --> 00:06:09.210
without needing to constantly monitor

109
00:06:09.210 --> 00:06:12.000
or manually trigger these tasks.

110
00:06:12.000 --> 00:06:13.410
So automation

111
00:06:13.410 --> 00:06:16.950
through Scheduled Tasks greatly reduces the workload

112
00:06:16.950 --> 00:06:18.990
for system administrators.

113
00:06:18.990 --> 00:06:21.960
Overall, Scheduled Tasks are used

114
00:06:21.960 --> 00:06:24.030
for automating system maintenance

115
00:06:24.030 --> 00:06:26.970
and management in Windows environments.

116
00:06:26.970 --> 00:06:30.450
By scheduling tasks to run at regular intervals,

117
00:06:30.450 --> 00:06:33.750
administrators can ensure the important processes

118
00:06:33.750 --> 00:06:38.730
like updates, backups, and scans are performed consistently

119
00:06:38.730 --> 00:06:40.230
and on time.

120
00:06:40.230 --> 00:06:44.490
This helps reduce human error, maintain system security,

121
00:06:44.490 --> 00:06:48.900
and free up administrators to focus on more critical tasks.

122
00:06:48.900 --> 00:06:51.600
Now let's conduct a demonstration

123
00:06:51.600 --> 00:06:54.240
of the Linux Cron capability.

124
00:06:54.240 --> 00:06:55.920
In this demonstration,

125
00:06:55.920 --> 00:07:00.870
I'm going to run a script every day at 7:00 AM

126
00:07:00.870 --> 00:07:04.260
using the crontab in Linux.

127
00:07:04.260 --> 00:07:07.980
To do this, I've created the following script.

128
00:07:07.980 --> 00:07:10.503
Let me go ahead and bring it up on the screen.

129
00:07:15.090 --> 00:07:18.270
This is a bash script that searches for

130
00:07:18.270 --> 00:07:21.960
and extracts log entries containing information

131
00:07:21.960 --> 00:07:25.200
about the NetworkManager service

132
00:07:25.200 --> 00:07:28.121
from the systems journal logs.

133
00:07:28.121 --> 00:07:29.451
The script begins

134
00:07:29.451 --> 00:07:34.451
by displaying the message Pulling NetworkManager entries

135
00:07:34.452 --> 00:07:38.173
to inform the user that the script is running.

136
00:07:38.173 --> 00:07:41.715
Next, the journalctl command is used

137
00:07:41.715 --> 00:07:45.870
to search the system's journal logs for entries specific

138
00:07:45.870 --> 00:07:49.350
to the NetworkManager service.

139
00:07:49.350 --> 00:07:54.350
Matching entries are processed using the cut command,

140
00:07:54.510 --> 00:07:58.680
extracting the first five fields of each line,

141
00:07:58.680 --> 00:08:01.140
which typically include the timestamp

142
00:08:01.140 --> 00:08:03.330
and initial log data.

143
00:08:03.330 --> 00:08:07.200
The resulting output is saved into a new file

144
00:08:07.200 --> 00:08:11.190
named netman-log.txt.

145
00:08:11.190 --> 00:08:16.190
Finally, the script prints NetworkManager log file created

146
00:08:16.740 --> 00:08:18.120
to inform the user

147
00:08:18.120 --> 00:08:21.390
that the log extraction process is complete

148
00:08:21.390 --> 00:08:24.390
and the new file has been created.

149
00:08:24.390 --> 00:08:28.830
This script automates the extraction of specific log entries

150
00:08:28.830 --> 00:08:30.960
for easier review.

151
00:08:30.960 --> 00:08:32.670
In this demonstration,

152
00:08:32.670 --> 00:08:36.723
I have created this netman-log.shscript,

153
00:08:37.650 --> 00:08:42.650
made it executable and stored it in my documents folder.

154
00:08:42.810 --> 00:08:45.540
To get it set up to run automatically,

155
00:08:45.540 --> 00:08:49.080
let's first open the Linux crontab editor

156
00:08:49.080 --> 00:08:53.943
with the following command, crontab -e.

157
00:08:55.980 --> 00:09:00.980
This is the crontab file where we can schedule tasks.

158
00:09:01.230 --> 00:09:06.230
We are going to add a line to this crontab editor

159
00:09:06.630 --> 00:09:09.030
to run our script.

160
00:09:09.030 --> 00:09:12.780
I've already added the line in here, but it's commented out,

161
00:09:12.780 --> 00:09:16.680
so I'll go ahead and uncomment it right here.

162
00:09:16.680 --> 00:09:19.530
This line will execute our script.

163
00:09:19.530 --> 00:09:23.400
Let's go through and understand exactly what it's doing.

164
00:09:23.400 --> 00:09:26.610
In the crontab for a scheduled task,

165
00:09:26.610 --> 00:09:31.110
there are five asterisk positions associated with the task.

166
00:09:31.110 --> 00:09:33.270
Here's what each one means.

167
00:09:33.270 --> 00:09:35.640
The first asterisk position

168
00:09:35.640 --> 00:09:39.150
specifies the exact minute the task will run,

169
00:09:39.150 --> 00:09:41.940
which in this case is zero minutes,

170
00:09:41.940 --> 00:09:44.043
meaning the start of the hour.

171
00:09:45.300 --> 00:09:48.810
The second asterisk position specifies the hour

172
00:09:48.810 --> 00:09:52.410
of execution, which in this case is the seventh hour

173
00:09:52.410 --> 00:09:55.023
of the day or 7:00 AM.

174
00:09:56.190 --> 00:10:01.190
The third position identifies a specific day of the month

175
00:10:03.540 --> 00:10:05.160
to run the task,

176
00:10:05.160 --> 00:10:07.740
which in this case is not being used,

177
00:10:07.740 --> 00:10:11.220
so the task will run on all days of the month.

178
00:10:11.220 --> 00:10:15.150
The fourth asterisk position identifies a month

179
00:10:15.150 --> 00:10:17.310
the task should execute,

180
00:10:17.310 --> 00:10:19.650
which in this case is not being used,

181
00:10:19.650 --> 00:10:23.010
so the task will run regardless of the month.

182
00:10:23.010 --> 00:10:26.610
The fifth asterisk position indicates a day

183
00:10:26.610 --> 00:10:29.610
of the week the task should execute,

184
00:10:29.610 --> 00:10:32.670
starting with day one as Monday.

185
00:10:32.670 --> 00:10:35.070
In this case, this asterisks position

186
00:10:35.070 --> 00:10:36.990
is also not being used,

187
00:10:36.990 --> 00:10:41.460
so the task will run every day Monday through Sunday.

188
00:10:41.460 --> 00:10:45.513
Now, let's exit and save the crontab editor.

189
00:10:46.830 --> 00:10:49.080
Next, we need to confirm

190
00:10:49.080 --> 00:10:52.080
that the cron job is successfully added

191
00:10:52.080 --> 00:10:57.060
by taking a look at the crontab and listing its contents.

192
00:10:57.060 --> 00:11:02.060
We'll do this with the following crontab -l command.

193
00:11:02.940 --> 00:11:06.660
And there we see, we can see the modified crontab,

194
00:11:06.660 --> 00:11:10.593
and we can see that our scheduled task is active.

195
00:11:11.490 --> 00:11:14.310
We've done now what we set out to do,

196
00:11:14.310 --> 00:11:17.190
which is to create a crontab entry

197
00:11:17.190 --> 00:11:21.360
to run a script every day at 7:00 AM

198
00:11:21.360 --> 00:11:23.910
but we want to see it happen.

199
00:11:23.910 --> 00:11:26.760
Let's start out by modifying the crontab

200
00:11:26.760 --> 00:11:30.000
to trigger our script within the next minute.

201
00:11:30.000 --> 00:11:35.000
We'll start off with the crontab -e command

202
00:11:36.810 --> 00:11:39.930
to be able to access our file.

203
00:11:39.930 --> 00:11:43.620
Now, looking up at the top right hand part of my screen,

204
00:11:43.620 --> 00:11:48.620
I see that the time just rolled over to 16:08.

205
00:11:49.050 --> 00:11:53.190
So knowing that the first asterisk position is the minute,

206
00:11:53.190 --> 00:11:55.680
I'm going to set that for minute nine,

207
00:11:55.680 --> 00:11:59.430
and I know that the second asterisk position is the hour.

208
00:11:59.430 --> 00:12:02.430
I'm going to set that for 16.

209
00:12:02.430 --> 00:12:04.833
Now let me save this and exit out.

210
00:12:06.450 --> 00:12:08.010
Now all we need to do

211
00:12:08.010 --> 00:12:10.920
is wait until the next minute clicks over,

212
00:12:10.920 --> 00:12:12.330
and if it works,

213
00:12:12.330 --> 00:12:16.920
we should see a netman-log.txt file appear

214
00:12:16.920 --> 00:12:19.080
in our documents folder.

215
00:12:19.080 --> 00:12:21.420
You can see in that folder right now,

216
00:12:21.420 --> 00:12:26.080
we just have our script, the netman-log.sh file.

217
00:12:27.870 --> 00:12:30.450
So we'll wait that one minute

218
00:12:30.450 --> 00:12:33.600
and then see that the text file shows up

219
00:12:33.600 --> 00:12:35.883
once the script is executed.

220
00:12:40.170 --> 00:12:43.680
Alright, it's just turned to 16:09.

221
00:12:43.680 --> 00:12:47.490
So let's check to see if our script executed.

222
00:12:47.490 --> 00:12:51.180
I'll list the file contents with the ls command.

223
00:12:51.180 --> 00:12:52.110
And there it is.

224
00:12:52.110 --> 00:12:57.110
You can see that our netman-log.txt file has been created.

225
00:12:59.970 --> 00:13:03.780
And you can see that it is populated with information.

226
00:13:03.780 --> 00:13:07.260
So we have just demonstrated the execution

227
00:13:07.260 --> 00:13:12.260
of a scheduled task with the crontab on a Linux machine.

228
00:13:12.660 --> 00:13:16.020
This is the end of our demonstration.

229
00:13:16.020 --> 00:13:21.020
So remember, Cron and Scheduled Tasks are tools

230
00:13:21.420 --> 00:13:24.330
used for automating tasks on Linux

231
00:13:24.330 --> 00:13:27.060
and Windows systems respectively.

232
00:13:27.060 --> 00:13:31.230
Cron is a job scheduler in Linux that allows users

233
00:13:31.230 --> 00:13:34.350
to automate tasks like security scans

234
00:13:34.350 --> 00:13:38.220
or log updates by specifying the exact time

235
00:13:38.220 --> 00:13:40.950
and frequency of execution.

236
00:13:40.950 --> 00:13:45.090
Scheduled Tasks in Windows serve a similar purpose,

237
00:13:45.090 --> 00:13:48.000
enabling automation for tasks such

238
00:13:48.000 --> 00:13:52.740
as running maintenance scripts or performing system updates.

239
00:13:52.740 --> 00:13:56.280
Both tools help system administrators ensure

240
00:13:56.280 --> 00:14:00.930
that tasks run consistently without manual intervention.

241
00:14:00.930 --> 00:14:04.380
This automation reduces human error,

242
00:14:04.380 --> 00:14:06.510
improves system security,

243
00:14:06.510 --> 00:14:09.753
and enables efficient system maintenance.

