1
00:00:00,720 --> 00:00:05,840
As you were able to see in the previous lectures our script is now running very smoothly.

2
00:00:05,850 --> 00:00:13,200
So what's left to be done there is to make this script run on the background as soon as the computer starts.

3
00:00:13,500 --> 00:00:19,070
Which means that the python program, the script that we built will run  and it will check for the time and

4
00:00:19,090 --> 00:00:22,250
then it will do something depending on what time it is.

5
00:00:22,350 --> 00:00:28,830
But at the moment the way we are running this is by opening a command line and then pointing to the

6
00:00:28,830 --> 00:00:34,500
script and that command line remains open as the script is runing.

7
00:00:34,590 --> 00:00:41,070
So probably we don't want that, we don't want to have the terminal open there all the time as we do work

8
00:00:41,070 --> 00:00:42,480
in our computer.

9
00:00:43,230 --> 00:00:50,670
So a better solution would be to find a way to run the script on the background and there exists a solution.

10
00:00:50,670 --> 00:00:50,790

11
00:00:50,790 --> 00:00:56,970
It's just that the workaround is different on Windows and on a Mac and Linux.

12
00:00:56,970 --> 00:01:03,060
So this lecture is about making the final touch to the program so that we can make it work on Windows

13
00:01:03,330 --> 00:01:08,070
to have a Python program we're on as a process in the background.

14
00:01:08,070 --> 00:01:14,100
So Linux users and Mac users can safely skip this lecture and go to the next one where you can

15
00:01:14,100 --> 00:01:17,150
implement these scripts as a background process.

16
00:01:17,370 --> 00:01:19,620
So yeah, hello Windows user.

17
00:01:19,620 --> 00:01:25,780
I'm on Windows 7 here but the solution should work on other versions of Windows as well.

18
00:01:26,550 --> 00:01:32,030
So how do we make a script for on a in the background a process?

19
00:01:32,490 --> 00:01:37,150
Well there's a simple trick you can do on Windows.

20
00:01:37,470 --> 00:01:45,710
You know you have a selection of Python there that you open for location.

21
00:01:46,260 --> 00:01:53,570
So when you were runing a program when you say Python script.py you are using this python.exe

22
00:01:54,030 --> 00:01:54,780
file

23
00:01:54,810 --> 00:02:01,780
which is the executable of the python program. Use this program to run your file. Now to run python

24
00:02:01,820 --> 00:02:10,600
files on the background you want to use Pythonw.exe which is specific subversion or Python

25
00:02:10,620 --> 00:02:17,060
for running Python scripts as a background and there is a simple trick you can do.

26
00:02:17,160 --> 00:02:23,580
You want to change the extension to pyw and once you do that

27
00:02:23,580 --> 00:02:25,380
now you can go ahead.

28
00:02:25,380 --> 00:02:27,520
So here is the file which extension

29
00:02:27,530 --> 00:02:34,680
I just change and what you can do now is just the double click this to execute it and there is no terminal

30
00:02:34,680 --> 00:02:35,210
showing.

31
00:02:35,220 --> 00:02:42,630
So the script should be running now and you can verify that in that task manager.

32
00:02:42,630 --> 00:02:50,520
So this one here which you can open by the way right click in here and or Start Task Manager and then

33
00:02:50,520 --> 00:02:56,770
go to processes and there you should be looking for python, for a process named Python here.

34
00:02:57,160 --> 00:02:59,240
And this time I expect this not to be here.

35
00:02:59,270 --> 00:03:05,570
So Python is not here so you should expect Pythonw.exe to be here.

36
00:03:06,220 --> 00:03:13,840
And the reason it is not there is that the script wasn't able to run because when you double click this

37
00:03:13,890 --> 00:03:17,860
you are double clicking you as a user not as an administrator.

38
00:03:18,030 --> 00:03:24,990
And if you're not executing it as an administrator you are not being able to access this to modify this

39
00:03:25,000 --> 00:03:31,710
hosts file because as you know from the previous lectures this is a protected file by Windows.

40
00:03:32,190 --> 00:03:40,800
But if you change these hosts file, let's change it to the hosts temp which is this local file here.

41
00:03:40,810 --> 00:03:42,620
So a fake hosts file.

42
00:03:42,840 --> 00:03:47,890
Well we don't need administrator rights to access it. Hosts temp.

43
00:03:47,930 --> 00:03:55,980
And now I go ahead and double click the Python file again.

44
00:03:56,690 --> 00:04:03,740
And now I should see Pythonw.exe in the processes as you see here.

45
00:04:03,960 --> 00:04:10,980
So you don't really see any output printed out but you can see maybe the results of this.

46
00:04:11,400 --> 00:04:19,440
So this is the hosts file which hasn't been added the domains in here because this says

47
00:04:19,440 --> 00:04:22,800
the working hours or between four and seven pm.

48
00:04:22,980 --> 00:04:25,560
Now it's 02:38.

49
00:04:25,640 --> 00:04:31,120
So let's change this to eight and 16.

50
00:04:32,250 --> 00:04:37,160
Save the script and go and stop this process because this is running 

51
00:04:37,760 --> 00:04:50,940
the old file and process and now run again the file and check the hosts file and now you should see these four

52
00:04:50,940 --> 00:04:55,000
lines which have been added in here.

53
00:04:55,010 --> 00:04:56,820
Great.

54
00:04:57,060 --> 00:05:02,080
Now let me stop this and process.

55
00:05:03,690 --> 00:05:07,570
And yeah, we are able to run our Python file as a process in the background.

56
00:05:07,600 --> 00:05:11,400
Now let's go ahead and implement the scheduling part.

57
00:05:11,400 --> 00:05:16,260
So we want to run this program as soon as the computer starts. To do that

58
00:05:16,300 --> 00:05:20,810
you want to go to task scheduler just there.

59
00:05:24,120 --> 00:05:32,090
And then create a base, create a task. Put a name for the task.

60
00:05:33,170 --> 00:05:38,050
I'm calling this a Website Blocker and configure for�

61
00:05:38,090 --> 00:05:39,690
I'm on a Windows 7.

62
00:05:39,980 --> 00:05:41,310
So I'm going to choose that.

63
00:05:41,360 --> 00:05:42,340
So Windows 7.

64
00:05:42,350 --> 00:05:47,990
And one important thing to do is you want to check this run with highest  privileges because if you don't

65
00:05:47,990 --> 00:05:54,190
do that, Windows will try to run the script from the normal user so you won't�

66
00:05:54,250 --> 00:05:57,060
In our case we need administrative rights.

67
00:05:57,440 --> 00:06:04,030
So check that and then go to Triggers and then New. So begin the task.

68
00:06:04,040 --> 00:06:05,630
When do you want to run the program.

69
00:06:05,620 --> 00:06:09,440
Well we want to run the program at startup.

70
00:06:09,530 --> 00:06:12,650
It should be fine.

71
00:06:13,120 --> 00:06:17,200
Then go to actions and you want to create a new action.

72
00:06:17,500 --> 00:06:24,920
So what you want to do well we want to start a program and normally want to point to your program script

73
00:06:26,180 --> 00:06:27,250
which is this one here.

74
00:06:27,270 --> 00:06:33,760
Website blocker.pyw. Open that. OK conditions.

75
00:06:34,230 --> 00:06:36,610
And you may want to uncheck this option.

76
00:06:36,650 --> 00:06:41,250
You also want to run the python program when you're on a battery on a laptop.

77
00:06:41,790 --> 00:06:43,210
And yeah, we should be good to go now.

78
00:06:43,210 --> 00:06:43,530

79
00:06:46,250 --> 00:06:46,740
Press

80
00:06:46,750 --> 00:06:47,980
OK.

81
00:06:48,470 --> 00:06:49,040
and

82
00:06:52,880 --> 00:07:00,090
here is the Website Blocker task which is ready to be run when the system starts up.

83
00:07:00,110 --> 00:07:05,210
So you may also want to disable it but that's for another case.

84
00:07:05,480 --> 00:07:12,350
And yeah, now when you start your computer the program should be running. Your Python loop will run

85
00:07:12,380 --> 00:07:13,480
every five seconds.

86
00:07:13,590 --> 00:07:19,790
The Python program will check of the hosts file and so on and so you may want to test to start to

87
00:07:19,790 --> 00:07:20,950
restart your program.

88
00:07:21,200 --> 00:07:29,720
Or you can go here and test it by clicking Run. Now the status is showing is running.

89
00:07:29,900 --> 00:07:34,960
And you may want to check if the task is really running or not.

90
00:07:35,450 --> 00:07:37,340
And it seems like it's not runing.

91
00:07:37,610 --> 00:07:39,950
So python.exe is not here.

92
00:07:40,120 --> 00:07:45,530
And the worst thing is that we don't even get any error now because the task, the Python process

93
00:07:45,560 --> 00:07:51,350
is running on the background and I know the reason why it's not running.

94
00:07:51,970 --> 00:08:00,780
You know if you see in the script now you notice that I'm still trying to modify the local hosts file who's

95
00:08:00,930 --> 00:08:04,490
path is being passed in here as a relative path.

96
00:08:04,490 --> 00:08:08,540
So this is not an absolute path like this one here.

97
00:08:09,550 --> 00:08:15,400
The problem with that is you know Windows is now executing this website blocker.pyw

98
00:08:15,410 --> 00:08:20,840
script is executing it from another directory.

99
00:08:20,870 --> 00:08:26,330
So it's not executing this from D Dropbox pp block website demo.

100
00:08:26,690 --> 00:08:31,460
Therefore it cannot find a file with this path.

101
00:08:31,460 --> 00:08:36,770
Therefore the solution to these is to actually pass the full path of the file here.

102
00:08:37,000 --> 00:08:40,540
Because as we are now Python is not trying to find the hosts file.

103
00:08:40,610 --> 00:08:46,380
So it's probably throwing an error that says the host file does not exist.

104
00:08:46,940 --> 00:08:54,640
So let's go ahead and copy this directory put it there.

105
00:08:54,640 --> 00:09:06,800
And of hosts. A backslash there and pass R prefix so that you can escape special characters.

106
00:09:06,860 --> 00:09:08,140
Save that.

107
00:09:08,410 --> 00:09:20,530
And yeah, now go to the task scheduler and this. Try to run again.

108
00:09:20,540 --> 00:09:26,480
So I'd say so running now. Let's see and yeah now this is running.

109
00:09:26,750 --> 00:09:33,030
So Pythonw.exe and you can also check the hosts file.

110
00:09:33,110 --> 00:09:39,840
So the lines are there because it's between 8:00 and 4:00 pm.

111
00:09:40,040 --> 00:09:44,360
So it's four to three the time.

112
00:09:44,870 --> 00:09:49,540
And yeah, the final thing we want to do is stop this very quickly.

113
00:09:49,710 --> 00:09:57,260
And change this to the real path. Hosts path. And this as well.

114
00:10:00,560 --> 00:10:05,700
Save it and go to the task scheduler.

115
00:10:07,250 --> 00:10:09,410
And add the current task.

116
00:10:09,950 --> 00:10:13,100
So it's ready now and runs again.

117
00:10:13,930 --> 00:10:18,440
Go to the processes. Pythonw.exe is there.

118
00:10:18,590 --> 00:10:19,840
So it's running.

119
00:10:20,270 --> 00:10:25,770
You may also want to check the horse fall which is in my notepad which says

120
00:10:25,850 --> 00:10:28,700
the file has been modified.

121
00:10:28,730 --> 00:10:32,170
Yeah I can see the lines there.

122
00:10:32,170 --> 00:10:39,890
The four websites that I want to block and maybe you are wondering why this first line is there.

123
00:10:39,880 --> 00:10:46,490
It should be separated in here and be there but the reason this line was up here was because the cursor

124
00:10:46,490 --> 00:10:51,150
was set to these point before executing the Python script.

125
00:10:51,160 --> 00:10:53,860
So you may want to set the cursor.

126
00:10:53,960 --> 00:10:56,610
Let me delete this.

127
00:10:57,740 --> 00:11:03,260
You may want to set the cursor in here before starting the python program.

128
00:11:03,340 --> 00:11:06,360
So before implementing this program.

129
00:11:07,220 --> 00:11:14,430
Now I saved it and I now expect this file to be refreshed by Python again.

130
00:11:14,480 --> 00:11:20,590
So let me close that and open it again.

131
00:11:20,720 --> 00:11:24,330
And now yeah, now this looks good.

132
00:11:24,400 --> 00:11:28,160
We have an empty linr there but that isn't a problem.

133
00:11:28,150 --> 00:11:33,440
So things will still work even with Facebook in here like it was earlier.

134
00:11:33,470 --> 00:11:40,800
Things should also work and everything looks to be working fine.

135
00:11:41,320 --> 00:11:43,200
I now cannot access Facebook.

136
00:11:43,200 --> 00:11:50,610
Between 8:00 a.m. and 4:00 p.m. and that's all about it.

137
00:11:50,800 --> 00:11:51,410
In the next lecture 

138
00:11:51,400 --> 00:11:54,280
I'm showing how to do the same.

139
00:11:54,290 --> 00:12:01,430
So how to run Python as a processes in the background for Linux and Mac users and that will end this section.

140
00:12:01,430 --> 00:12:01,880

141
00:12:02,360 --> 00:12:08,500
So hope you enjoyed it and I hope you make the most of this program for you or use your daily use.

142
00:12:08,510 --> 00:12:08,800

143
00:12:09,020 --> 00:12:15,160
If something isn't working just drop me a question or a message and yeah I'll be happy to help you.

144
00:12:15,380 --> 00:12:21,770
Or even if you have questions regarding the script so I know this has been lots of indentation here.

145
00:12:22,250 --> 00:12:28,460
Probably you'll get some errors there and probably that error is because of indentation so be careful with indenting

146
00:12:28,460 --> 00:12:30,800
your lines.

147
00:12:30,890 --> 00:12:37,780
And yeah, I'll close this now and I'll see you in the next lectures.

148
00:12:38,300 --> 00:12:38,570
Thanks.

