WEBVTT

00:00.080 --> 00:01.940
The final project is now done.

00:01.940 --> 00:06.770
We have the two Python programs, but I thought it would be nice to show you how you can launch, or

00:06.770 --> 00:10.850
how you can actually automatically launch your programs when the Raspberry Pi boots.

00:10.850 --> 00:14.240
And here in this lesson, this is actually something completely new that you're going to see.

00:14.240 --> 00:16.310
So it's not something we covered in the course.

00:16.520 --> 00:17.720
It's really a bonus step.

00:17.720 --> 00:22.490
If you want to go a bit further, we're going to learn a new functionality that's quite useful on the

00:22.490 --> 00:23.240
Raspberry Pi.

00:23.240 --> 00:30.140
So let's come back to the we have the two terminals and you see that I run my project step four and

00:30.140 --> 00:32.960
then I run my project step six.

00:32.960 --> 00:33.980
Those are the two files.

00:33.980 --> 00:35.600
And the problem with that.

00:35.600 --> 00:36.950
So it's correctly working.

00:36.950 --> 00:42.860
But every time you want to start your project you have to, well, get access to the Raspberry Pi.

00:42.860 --> 00:47.660
So you need to either like I'm doing here, login with VNC or plug a screen.

00:47.660 --> 00:54.230
Or even if you don't want to have any desktop access, you can just log in with SSH and run the program.

00:54.230 --> 00:55.850
But you will always have to do that.

00:55.850 --> 01:02.090
Now there is a way for you to start this project without even Accessing the Raspberry Pi.

01:02.120 --> 01:05.120
So the only thing you need to do is to boot the Raspberry Pi.

01:05.120 --> 01:07.850
And the project is going to start automatically.

01:07.850 --> 01:10.010
And let's learn how to do this.

01:10.010 --> 01:13.550
So I'm going to I'm just going to use one terminal.

01:14.270 --> 01:15.410
Let's clear everything.

01:15.410 --> 01:19.580
And we are going to use the functionality that's called systemd.

01:19.610 --> 01:25.100
And here for this video I really emphasize that you have to just just follow every step that I do.

01:25.130 --> 01:25.310
Okay.

01:25.340 --> 01:28.550
It's very important to follow exactly the things that I do.

01:28.550 --> 01:32.600
So you will go to slash lib okay.

01:32.630 --> 01:34.640
We start from the root of the file system.

01:34.640 --> 01:44.060
So slash lib slash system D slash system okay.

01:44.090 --> 01:44.990
That should work.

01:44.990 --> 01:51.590
And if I do ls uh, well, you see, we have a lot of files and lots of them.

01:51.590 --> 01:54.410
You can see they end with dot service.

01:54.410 --> 02:00.260
So all of those files basically are programs that are going to be run automatically, for example,

02:00.290 --> 02:00.890
on boot.

02:00.890 --> 02:03.140
And they will be run in the background.

02:03.140 --> 02:07.400
So you don't necessarily know what's happening, but everything is running in the background.

02:07.400 --> 02:10.460
And what we will do is we will simply add a new service.

02:10.460 --> 02:16.040
So actually two new services, one for the first program and one for the second program, so that we

02:16.040 --> 02:18.800
can just start them on boot.

02:18.800 --> 02:24.050
And to do that we simply need to add new files in this here in this directory.

02:24.050 --> 02:30.350
And so I'm going to add a new file with a nano directly because I'm going to write to it and note that

02:30.350 --> 02:36.590
I'm not in my home directory anymore okay I am not in slash home slash pi slash something.

02:36.590 --> 02:41.150
So because I'm the you see I'm logged in at the as the user Pi.

02:41.150 --> 02:45.560
But I'm in another directory that's not in my home directory.

02:45.560 --> 02:48.530
I will need the admin privilege.

02:48.530 --> 02:54.080
So I will need to write sudo in front of comments if I want to, for example, modify files to remove

02:54.080 --> 03:05.990
files, etc. so I will do sudo nano and then let's name this project take Photos and then that service.

03:06.020 --> 03:09.080
Okay, so you need to end with that service.

03:09.140 --> 03:10.880
I'm going to press enter.

03:10.910 --> 03:12.620
Let's make it a bit bigger.

03:16.340 --> 03:20.960
And here where you're going to need to write exactly what I'm writing.

03:20.990 --> 03:21.620
Okay.

03:21.620 --> 03:30.380
So you're going to start with brackets like that and write unit then description equal.

03:30.380 --> 03:32.240
And here you can write whatever you want okay.

03:32.270 --> 03:47.420
For example program that detects movement and then takes a photo and send it by email.

03:47.420 --> 03:49.430
So this is just a description.

03:49.430 --> 03:52.010
It's not going to be used by anything okay.

03:52.040 --> 03:52.820
It's just for you.

03:52.850 --> 03:56.780
Maybe later if you find a service again and you know what it does.

03:56.780 --> 04:02.480
But that's not really important, then you're going to write after okay with uppercase at the beginning

04:02.490 --> 04:08.250
and no space multi user dot targets.

04:08.250 --> 04:12.270
So this is basically going to say when to start the service.

04:12.300 --> 04:12.480
Okay.

04:12.510 --> 04:15.630
You have different things that are going to be executed on boot.

04:15.630 --> 04:17.850
And there is a certain order to respect.

04:17.850 --> 04:20.520
I'm not going to go much further with this okay.

04:20.550 --> 04:23.640
This is something you can explore on your own after this course.

04:23.640 --> 04:28.830
If you want to, you can explore more about systemd and learn what's the multi user target.

04:28.830 --> 04:31.410
But for now let's just write it like this okay.

04:31.440 --> 04:34.320
So multi user dot target.

04:34.500 --> 04:35.760
When I go back to a new line.

04:35.760 --> 04:46.380
And then I'm going to do service inside bracket, I will have to put working directory with a W and

04:46.380 --> 04:47.490
D uppercase.

04:48.960 --> 04:54.030
And here I'm going to put the directory where I have my files.

04:54.030 --> 04:57.210
So here slash home slash.

04:57.210 --> 05:03.000
If you are not sure you just open a new terminal Here.

05:03.060 --> 05:09.720
I'm going to my document Python programs and I do PWD.

05:10.680 --> 05:16.980
And then I can just copy this inside the file and how to copy.

05:17.010 --> 05:20.400
You can do control shift and C okay.

05:20.430 --> 05:24.450
Because just Ctrl C you see Ctrl C is sending Ctrl C to the terminal.

05:24.570 --> 05:31.380
So Ctrl shift and C and here Ctrl shift and V.

05:31.650 --> 05:32.100
All right.

05:32.100 --> 05:33.750
And this way you don't make any mistake.

05:33.750 --> 05:36.720
So that's the working directory for our script.

05:36.810 --> 05:40.740
So that we can also run this as the Pi user.

05:40.770 --> 05:42.120
That's going to be very important.

05:42.120 --> 05:50.700
Then we have exec start like this is equal to an exec start is the command we want to run.

05:50.880 --> 05:53.670
So a service is not necessarily going to run a Python script.

05:53.670 --> 05:56.490
It's going to run any command that you want.

05:56.520 --> 06:02.400
Now the command we want to run here is Python three project.

06:02.400 --> 06:04.170
Step four dot p y.

06:04.200 --> 06:06.660
But for this we are going to use the absolute path.

06:06.660 --> 06:12.390
So we're going to use the absolute path for the file name here but also for the command.

06:12.420 --> 06:15.330
And you might think what's the absolute path for Python three.

06:15.330 --> 06:18.090
It's already a it's just a comment right.

06:18.090 --> 06:21.480
But even comments have an absolute path.

06:21.480 --> 06:24.480
And you can find that with the command which.

06:24.780 --> 06:27.900
So you do which Python three.

06:27.900 --> 06:30.000
And you see that when you run Python three.

06:30.030 --> 06:35.490
Actually what's running is slash user slash bin slash Python three.

06:35.520 --> 06:35.880
All right.

06:35.880 --> 06:39.210
So this is kind of an alias for that one.

06:39.210 --> 06:46.080
So I'm going to copy again and paste it there with Ctrl shift and C and Ctrl shift and V.

06:46.260 --> 06:46.590
All right.

06:46.620 --> 06:48.270
So error in Python three.

06:48.270 --> 06:50.730
And then what.

06:50.760 --> 06:59.280
That's the same path here for the Python program that I'm going to copy here.

07:00.510 --> 07:10.680
And then slash and the name of the file is project step four dot p y.

07:10.710 --> 07:13.170
Make sure you don't make any typo.

07:13.170 --> 07:20.250
If you want, you can actually just copy that and run it inside the terminal and see if it's going to

07:20.250 --> 07:20.970
work.

07:22.050 --> 07:24.150
And you see it's working.

07:24.390 --> 07:24.630
All right.

07:24.660 --> 07:27.210
So you can be sure that you have the correct command.

07:27.240 --> 07:28.530
That's very important.

07:28.560 --> 07:29.160
Good.

07:29.190 --> 07:30.450
Now I go back to a new line.

07:30.450 --> 07:31.860
I have one more thing here.

07:31.860 --> 07:34.830
User equal pi.

07:34.860 --> 07:35.220
Okay.

07:35.250 --> 07:40.050
So pi is pi because we are here connected as the user pi.

07:40.080 --> 07:42.780
So that's the user you want to run the script as.

07:42.810 --> 07:43.170
Right.

07:43.200 --> 07:48.030
So if you used something different than pi you need to also replace the user here.

07:48.030 --> 07:49.530
And we are almost done.

07:49.530 --> 07:59.220
So finally this one last thing is going to be install like that and wanted by okay.

07:59.250 --> 08:00.630
So be uppercase.

08:00.630 --> 08:07.590
And we're going to write the same thing here multi That's user dot target.

08:07.680 --> 08:08.070
All right.

08:08.070 --> 08:09.720
So it's quite a lot of new things.

08:09.720 --> 08:11.670
Once again I'm not going to explain everything.

08:11.670 --> 08:15.000
That's something you can search on your own after the course.

08:15.000 --> 08:17.220
But now the file is finished.

08:17.220 --> 08:21.810
So I'm going to do Ctrl S and then Ctrl x.

08:21.810 --> 08:26.220
And I have my new file that's going to be saved inside inside here.

08:26.220 --> 08:31.320
And I'm going to directly create a service for the second Python program.

08:31.320 --> 08:32.970
So that's going to be the project.

08:33.390 --> 08:34.710
Step six.

08:34.710 --> 08:36.360
Here we have run the project step four.

08:36.360 --> 08:42.660
And we want the project step six to do this is going to be much simpler because I already created a

08:42.690 --> 08:44.700
service is going to be almost the same.

08:44.700 --> 08:46.680
So I can just copy it.

08:46.770 --> 08:51.660
To copy it I'm going to also need to do sudo because I'm not in my home directory.

08:52.320 --> 08:57.960
So sudo cp project take photos service.

08:57.960 --> 09:00.270
That's the one I want to copy from.

09:00.660 --> 09:08.190
And the new file is going to be for example project Web server dot service.

09:08.190 --> 09:11.220
So that's the name for my second service.

09:11.550 --> 09:19.710
Then I do sudo nano project web server.

09:21.330 --> 09:22.380
Okay, that's the same one.

09:22.380 --> 09:27.930
And what I will do is modify the description first because it's not accurate.

09:28.440 --> 09:41.580
So this is going to be program actually uses a web server that displays last photo.

09:41.610 --> 09:42.930
It's really not important.

09:42.930 --> 09:45.000
So unit is the same description.

09:45.000 --> 09:47.520
We can change it after it's going to be the same.

09:47.520 --> 09:52.920
And then the working directory is the same because the Python file is still in that one.

09:52.920 --> 09:58.710
But the exact start is going to be different only with the file name here you're going to be project

09:58.710 --> 10:00.420
step six.

10:00.660 --> 10:01.020
Okay.

10:01.050 --> 10:04.140
So that's the only thing we need to change is the exact start.

10:04.170 --> 10:05.920
So we don't run the same script.

10:05.950 --> 10:06.340
All right.

10:06.370 --> 10:07.660
The rest is the same.

10:07.690 --> 10:11.860
Now let's do control S and X, and we have the two services.

10:11.890 --> 10:12.460
All right.

10:12.460 --> 10:19.270
So now that we have created those files we will be able to start and stop the service.

10:19.270 --> 10:22.060
So make sure that you don't have anything running.

10:22.090 --> 10:22.660
Okay.

10:22.690 --> 10:28.150
Make sure that you are not running those files anywhere else on your Raspberry Pi.

10:28.180 --> 10:31.090
Then let's go back to the home directory for example.

10:31.120 --> 10:32.140
Let's do clear.

10:32.140 --> 10:34.030
And now how to start the service.

10:34.060 --> 10:37.870
We can do sudo systemctl.

10:37.900 --> 10:38.200
Okay.

10:38.230 --> 10:44.140
So we have the systemctl command with start and then the name of the service.

10:44.140 --> 10:49.150
So for example project and then take photos.

10:49.150 --> 10:50.440
So I can press tab here.

10:50.440 --> 10:51.610
It should be found.

10:51.640 --> 10:52.030
Okay.

10:52.060 --> 10:57.850
So if it's correctly placed in the right folder with the right name and everything we should find it

10:57.850 --> 10:58.150
here.

10:58.150 --> 11:01.600
If I press enter you see that we go back to a new line.

11:01.600 --> 11:02.500
Nothing happens.

11:02.500 --> 11:06.370
But the program was actually started And you can check that.

11:06.370 --> 11:11.230
For example, the easy check is just to put your hand in front of the Pir sensor and you should see

11:11.230 --> 11:16.180
the LED should be turned on, and then you can put your hand for five seconds.

11:16.180 --> 11:20.920
And you will also see new photos in the photos.

11:20.920 --> 11:25.600
Final project okay, a new log file and you will also receive an email.

11:25.600 --> 11:37.060
I can start my second program with pseudo system CTL start project with a web server.

11:38.020 --> 11:42.280
And this is going to start the second Python program, this one.

11:42.280 --> 11:43.120
How to test it.

11:43.150 --> 11:48.760
Well you just go to the URL here and you see it's working okay.

11:48.760 --> 11:51.910
So note I didn't run any Python script directly.

11:51.910 --> 11:53.950
I just started the services.

11:53.980 --> 11:55.450
Now if you have an error.

11:55.450 --> 12:01.150
So for example if you cannot see the LED, if you cannot see new photos, if you cannot see that you

12:01.150 --> 12:05.830
can use the journal Settle.

12:05.830 --> 12:07.720
So you don't need sudo for that.

12:07.720 --> 12:12.070
And then dash u and the name of the service for example that one.

12:14.500 --> 12:20.950
I press enter and well that's actually not from today.

12:20.950 --> 12:24.160
But the last time I, I tried this I had some error.

12:24.190 --> 12:24.400
Okay.

12:24.430 --> 12:27.280
So if you have some error logs then.

12:27.310 --> 12:29.980
So I press Q here or ctrl C to exit.

12:29.980 --> 12:36.460
So if you have some errors and you are not sure what you can use this Journalctl u and then the name

12:36.460 --> 12:37.390
of the service.

12:37.840 --> 12:38.260
Okay.

12:38.260 --> 12:41.680
But if it's working fine you will not see anything in this file.

12:41.770 --> 12:43.810
It's just for you to to know.

12:43.840 --> 12:46.390
So let's, uh, clear that.

12:46.390 --> 12:51.880
And now, while those programs are running in the background as services, how to stop them, you can

12:51.880 --> 12:54.610
do sudo systemctl.

12:54.610 --> 12:55.780
And we had the start.

12:55.780 --> 13:06.730
So we have stop project take photos and then we do the same with project web server Seven and they are

13:06.730 --> 13:07.390
stopped.

13:07.750 --> 13:10.540
And you can see if I refresh this page here.

13:12.100 --> 13:13.960
Now, you see it's not working anymore.

13:13.960 --> 13:18.730
And if you put your hand in front of the Pir sensor, you will not see the LED anymore.

13:18.730 --> 13:21.640
So you can see that the scripts have been stopped.

13:21.640 --> 13:26.050
And so you can do start and stop just like that.

13:26.080 --> 13:26.620
Great.

13:26.620 --> 13:28.810
And now you might think that that's great.

13:28.810 --> 13:33.160
Instead of starting the Python script directly, we can start them with the service.

13:33.160 --> 13:35.260
But it's still the same as before.

13:35.260 --> 13:42.670
And so yes, but now what we can do is we can enable those services so that they will be started automatically

13:42.670 --> 13:43.420
on boot.

13:43.630 --> 13:45.160
So that's why we create a service.

13:45.160 --> 13:48.040
And before we enable the service there is a command you can use.

13:48.040 --> 13:57.370
So sudo systemctl it's called list dash unit dash files.

13:57.820 --> 13:59.890
Let's run that and you can see.

13:59.890 --> 14:01.390
So we will have a lot of stuff.

14:01.420 --> 14:04.450
And you can see the kind of file and then the state.

14:04.450 --> 14:05.830
So we have state and presets.

14:05.860 --> 14:12.730
What's going to be interesting for us is state and you can see some stuff is disabled, some stuff is

14:12.730 --> 14:13.750
enabled.

14:13.780 --> 14:14.440
Okay.

14:14.980 --> 14:18.400
If I press enter I can scroll here.

14:18.490 --> 14:24.340
And I'm just going to do Ctrl C and I'm going to run the command again.

14:24.340 --> 14:26.830
But this time I can just do pipe.

14:26.830 --> 14:28.930
So we've done this before in this course.

14:28.930 --> 14:34.750
If you do pipe, basically the whole content of the left is going to be on the right.

14:34.750 --> 14:38.140
And you can for example, do grep to filter the content.

14:38.560 --> 14:40.930
And I'm going to do grep project.

14:42.940 --> 14:48.190
So it's going to filter and only print the lines that correspond to this here on the right.

14:48.190 --> 14:49.180
So that's quite useful.

14:49.180 --> 14:51.070
And we can find our two services.

14:51.070 --> 14:53.470
So you see with this you can find the services.

14:53.470 --> 15:00.730
And then you see what's interesting for us is that first column here so disabled and disabled.

15:00.760 --> 15:05.020
This means that if you start the Raspberry Pi again nothing's going to happen.

15:05.020 --> 15:06.490
But now you can enable them.

15:06.490 --> 15:11.500
So sudo systemctl enable.

15:11.530 --> 15:12.970
Okay so the commands are quite simple.

15:12.970 --> 15:17.200
You have start and stop to directly start and stop them right away.

15:17.200 --> 15:23.170
And you have enabled to well, enable the service to be started on the next boot and every other boot

15:23.170 --> 15:28.030
and let's do project take photos service.

15:28.030 --> 15:30.730
You see it's going to create a symlink somewhere.

15:30.760 --> 15:38.530
And now if I do this again you see that this one is enabled okay I'm going to do the same thing.

15:38.530 --> 15:45.190
So I'm going to do enable project with web server.

15:46.210 --> 15:49.030
And now they are enabled okay.

15:49.060 --> 15:55.030
So what I recommend you is when you create a new system like that, you can use the start and stop just

15:55.030 --> 15:57.070
to make sure that everything is working.

15:57.070 --> 16:00.880
When everything is working, then you can enable them and how to test that.

16:00.910 --> 16:04.390
Well I'm going to do a sudo reboot.

16:04.390 --> 16:06.550
So let's reboot the Raspberry Pi.

16:06.550 --> 16:11.900
And well as we reboot the Raspberry Pi, we also lose the connection to VNC because I was connected

16:11.900 --> 16:19.700
with VNC and now you can already see actually if it's working because the LED should turn on if you

16:19.700 --> 16:22.100
put your hand close to the Pir sensor.

16:22.130 --> 16:22.520
Okay.

16:22.550 --> 16:26.540
We didn't even connect to the Raspberry Pi, but if you see this, it means that the program is already

16:26.540 --> 16:30.560
running on boot, so you don't need to connect to the Raspberry Pi to start it.

16:30.890 --> 16:39.440
And then also I just opened a web browser and note that I opened this web browser here on my windows

16:39.440 --> 16:40.250
computer.

16:40.250 --> 16:44.180
So this computer is connected to the same network as the Raspberry Pi.

16:44.210 --> 16:45.350
It's very important.

16:45.380 --> 16:51.230
I could have opened that also from my smartphone if I wanted to, and I just typed the IP address of

16:51.230 --> 16:57.620
the Raspberry Pi and then the port, and then slash check photos and you see we have no photo available.

16:57.620 --> 16:59.180
So it's actually working.

16:59.180 --> 17:05.930
You see, I can get access to that web page, well, basically from any web browser that's in the network.

17:05.960 --> 17:07.820
And then if I take a photo.

17:07.880 --> 17:09.680
So let's refresh.

17:10.640 --> 17:12.410
You see, we have one new photo.

17:12.410 --> 17:13.820
So I just took a photo.

17:15.350 --> 17:16.940
Well, that's just my hand.

17:16.970 --> 17:18.320
I was doing some movement.

17:18.320 --> 17:20.420
And you see also the path to the photo.

17:20.420 --> 17:21.950
So it's correctly working.

17:21.950 --> 17:25.550
And we didn't even have to access the Raspberry Pi ourselves.

17:25.550 --> 17:27.980
So this is a quite powerful functionality.

17:28.190 --> 17:30.950
You can develop your application and then you create a service.

17:30.950 --> 17:34.310
And then it's just going to run when you boot the Raspberry Pi.

17:34.340 --> 17:39.440
Now let's connect again to VNC and let's get back to the Raspberry Pi.

17:39.950 --> 17:46.640
So I am back here and I'm just going to show you one last thing is how to disable the services.

17:46.640 --> 17:52.010
Because now that you have created the services and that they are enabled, they will run every time

17:52.010 --> 17:53.330
you boot your Raspberry Pi.

17:53.360 --> 17:53.990
So that's nice.

17:53.990 --> 17:55.760
But what if you want to stop that?

17:55.790 --> 17:59.540
Well I'm just going to show you this to conclude the project here.

17:59.540 --> 18:08.630
So first of all let's do again sudo systemctl with list unit Files.

18:08.630 --> 18:13.520
And let's do pipe grep to sort and with the project.

18:13.520 --> 18:15.050
And we see that it's enabled.

18:15.050 --> 18:20.450
So I can do sudo systemctl and you probably guessed the command.

18:20.450 --> 18:25.730
So if we have enable to enable something we have disabled okay.

18:25.760 --> 18:30.080
Quite easy to disable project take photo.

18:30.440 --> 18:32.720
You see it's removing something here.

18:33.020 --> 18:37.910
And I can do the same with project web server.

18:37.940 --> 18:38.330
Okay.

18:38.360 --> 18:44.360
And now if I list again you see those two are disabled.

18:44.360 --> 18:48.800
And very important note they are disabled for the next boot okay.

18:48.830 --> 18:50.870
Which means that they are actually still running.

18:50.870 --> 18:52.220
If you check they are still running.

18:52.220 --> 18:57.200
So if you want to stop them you will need to do sudo systemctl stop okay.

18:57.230 --> 19:04.040
When you enable disable it's not starting or stopping the program, it's just enabling or disabling

19:04.040 --> 19:06.770
the run of the program for the next boot.

19:06.800 --> 19:07.100
All right.

19:07.130 --> 19:08.900
So that's a very important difference.
