1
1

00:00:00,640  -->  00:00:02,960
<v ->Hi guys and welcome back. In this video</v>
2

2

00:00:02,960  -->  00:00:05,990
we're going to be creating a job that runs every hour,
3

3

00:00:05,990  -->  00:00:09,470
that we check our alerts and send out emails if necessary.
4

4

00:00:09,470  -->  00:00:12,320
Basically all it's gonna do is run our alert updater.
5

5

00:00:12,320  -->  00:00:15,373
So anything that does, this will do every hour.
6

6

00:00:16,330  -->  00:00:21,140
The first thing to do is to type "sudo vi/etc/crontab".
7

7

00:00:21,140  -->  00:00:25,900
And this file is what that finds jobs that run periodically.
8

8

00:00:25,900  -->  00:00:28,120
What we've got is
9

9

00:00:28,120  -->  00:00:30,020
the minute,
10

10

00:00:30,020  -->  00:00:31,270
hour,
11

11

00:00:31,270  -->  00:00:32,970
day of the month,
12

12

00:00:32,970  -->  00:00:34,760
the month number,
13

13

00:00:34,760  -->  00:00:36,340
and day of the week
14

14

00:00:36,340  -->  00:00:37,890
that a job will run at.
15

15

00:00:37,890  -->  00:00:40,620
A star means all of them.
16

16

00:00:40,620  -->  00:00:45,520
So for example, this job here, runs on the 17th minute
17

17

00:00:45,520  -->  00:00:48,240
of every hour, every day of the month,
18

18

00:00:48,240  -->  00:00:50,823
every month of the year, and every day of the week.
19

19

00:00:52,060  -->  00:00:56,730
It is ran by the root user and the command is this one here.
20

20

00:00:56,730  -->  00:00:59,760
It goes into the root folder of our server
21

21

00:00:59,760  -->  00:01:03,767
and does run parts report if you see "cron.hourly".
22

22

00:01:05,060  -->  00:01:06,350
Then we've got another job here
23

23

00:01:06,350  -->  00:01:09,400
that runs on the 25th minute of the sixth hour
24

24

00:01:09,400  -->  00:01:12,253
of the day, every day of the year.
25

25

00:01:13,290  -->  00:01:16,970
Then we've got another one that only runs on Sunday.
26

26

00:01:16,970  -->  00:01:17,970
And then we've got another one
27

27

00:01:17,970  -->  00:01:20,220
that only runs on the first day of the month.
28

28

00:01:21,860  -->  00:01:26,150
We're gonna add one that runs, for example, on the
29

29

00:01:26,150  -->  00:01:27,600
29th minute
30

30

00:01:28,660  -->  00:01:31,670
of every hour, of every day,
31

31

00:01:31,670  -->  00:01:34,940
of every month, of every day of the week.
32

32

00:01:34,940  -->  00:01:36,280
It's gonna run
33

33

00:01:36,280  -->  00:01:38,840
by our Jose user
34

34

00:01:38,840  -->  00:01:40,827
and the command is gonna be
35

35

00:01:40,827  -->  00:01:43,640
"cd" into our folder
36

36

00:01:45,080  -->  00:01:48,730
and is gonna run "pipenv". So what this'll do
37

37

00:01:48,730  -->  00:01:52,050
is it'll go into our folder "pricing-serv".
38

38

00:01:52,050  -->  00:01:54,670
Make sure to spell things correctly, "pricing service".
39

39

00:01:54,670  -->  00:01:58,147
It'll go into our folder and it will run "pipenv".
40

40

00:01:59,680  -->  00:02:01,760
So where we go into this folder here
41

41

00:02:01,760  -->  00:02:06,230
and what this does in bash syntax is it will do this first
42

42

00:02:06,230  -->  00:02:09,060
and if it succeeds, then it will do that.
43

43

00:02:09,060  -->  00:02:11,650
So we'll go into our folder and then
44

44

00:02:11,650  -->  00:02:15,560
it will run "pipenv", which is in our local folder
45

45

00:02:15,560  -->  00:02:17,390
that you have to spell correctly as well
46

46

00:02:17,390  -->  00:02:21,107
with a full stop in front, and then it will run
47

47

00:02:21,107  -->  00:02:24,017
"pythan alert_updater.py".
48

48

00:02:25,260  -->  00:02:26,093
That's it.
49

49

00:02:26,093  -->  00:02:28,710
Because we're running it from this folder,
50

50

00:02:28,710  -->  00:02:31,110
it is just as if we were running it ourselves
51

51

00:02:31,110  -->  00:02:32,840
inside that folder so it will know
52

52

00:02:32,840  -->  00:02:35,510
where our models are and all that stuff.
53

53

00:02:35,510  -->  00:02:39,320
So once you save and quit, this'll begin running
54

54

00:02:39,320  -->  00:02:42,053
every hour on the minute that we've specified.
55

55

00:02:43,290  -->  00:02:46,030
Something that you should do while you're doing
56

56

00:02:46,030  -->  00:02:48,980
this "crontab" thing is you should
57

57

00:02:50,220  -->  00:02:51,600
copy it whole
58

58

00:02:52,780  -->  00:02:54,830
and then when you quit you should run it
59

59

00:02:54,830  -->  00:02:56,890
to make sure that it actually works.
60

60

00:02:56,890  -->  00:02:59,440
And it seems to work. We haven't added any alerts yet,
61

61

00:02:59,440  -->  00:03:01,510
but as you add them, they'll start to be sent.
62

62

00:03:01,510  -->  00:03:05,140
Of course you do have to add your ".env" file.
63

63

00:03:05,140  -->  00:03:08,030
Make sure to look at the ".env.example" file
64

64

00:03:08,030  -->  00:03:10,780
and add a ".env" file that matches this.
65

65

00:03:10,780  -->  00:03:13,070
Otherwise you won't be able to send any emails
66

66

00:03:13,070  -->  00:03:16,083
since you won't have the mail gun information that you need.
67

67

00:03:17,480  -->  00:03:20,550
But, that's it. Setting up a regularly scheduled job
68

68

00:03:20,550  -->  00:03:23,370
is really straight-forward using Ubuntu
69

69

00:03:23,370  -->  00:03:26,990
just modify your "crontab" and specify when you wanna run it
70

70

00:03:26,990  -->  00:03:29,170
and which user you want it run as
71

71

00:03:29,170  -->  00:03:31,400
and which command you wanna run.
72

72

00:03:31,400  -->  00:03:32,930
Thanks for joining me in this video
73

73

00:03:32,930  -->  00:03:34,330
and see you in the next one.
