The final project is now done.
We have the two Python programs, but I thought it would be nice to show you how you can launch, or
how you can actually automatically launch your programs when the Raspberry Pi boots.
And here in this lesson, this is actually something completely new that you're going to see.
So it's not something we covered in the course.
It's really a bonus step.
If you want to go a bit further, we're going to learn a new functionality that's quite useful on the
Raspberry Pi.
So let's come back to the we have the two terminals and you see that I run my project step four and
then I run my project step six.
Those are the two files.
And the problem with that.
So it's correctly working.
But every time you want to start your project you have to, well, get access to the Raspberry Pi.
So you need to either like I'm doing here, login with VNC or plug a screen.
Or even if you don't want to have any desktop access, you can just log in with SSH and run the program.
But you will always have to do that.
Now there is a way for you to start this project without even Accessing the Raspberry Pi.
So the only thing you need to do is to boot the Raspberry Pi.
And the project is going to start automatically.
And let's learn how to do this.
So I'm going to I'm just going to use one terminal.
Let's clear everything.
And we are going to use the functionality that's called systemd.
And here for this video I really emphasize that you have to just just follow every step that I do.
Okay.
It's very important to follow exactly the things that I do.
So you will go to slash lib okay.
We start from the root of the file system.
So slash lib slash system D slash system okay.
That should work.
And if I do ls uh, well, you see, we have a lot of files and lots of them.
You can see they end with dot service.
So all of those files basically are programs that are going to be run automatically, for example,
on boot.
And they will be run in the background.
So you don't necessarily know what's happening, but everything is running in the background.
And what we will do is we will simply add a new service.
So actually two new services, one for the first program and one for the second program, so that we
can just start them on boot.
And to do that we simply need to add new files in this here in this directory.
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
I'm not in my home directory anymore okay I am not in slash home slash pi slash something.
So because I'm the you see I'm logged in at the as the user Pi.
But I'm in another directory that's not in my home directory.
I will need the admin privilege.
So I will need to write sudo in front of comments if I want to, for example, modify files to remove
files, etc. so I will do sudo nano and then let's name this project take Photos and then that service.
Okay, so you need to end with that service.
I'm going to press enter.
Let's make it a bit bigger.
And here where you're going to need to write exactly what I'm writing.
Okay.
So you're going to start with brackets like that and write unit then description equal.
And here you can write whatever you want okay.
For example program that detects movement and then takes a photo and send it by email.
So this is just a description.
It's not going to be used by anything okay.
It's just for you.
Maybe later if you find a service again and you know what it does.
But that's not really important, then you're going to write after okay with uppercase at the beginning
and no space multi user dot targets.
So this is basically going to say when to start the service.
Okay.
You have different things that are going to be executed on boot.
And there is a certain order to respect.
I'm not going to go much further with this okay.
This is something you can explore on your own after this course.
If you want to, you can explore more about systemd and learn what's the multi user target.
But for now let's just write it like this okay.
So multi user dot target.
When I go back to a new line.
And then I'm going to do service inside bracket, I will have to put working directory with a W and
D uppercase.
And here I'm going to put the directory where I have my files.
So here slash home slash.
If you are not sure you just open a new terminal Here.
I'm going to my document Python programs and I do PWD.
And then I can just copy this inside the file and how to copy.
You can do control shift and C okay.
Because just Ctrl C you see Ctrl C is sending Ctrl C to the terminal.
So Ctrl shift and C and here Ctrl shift and V.
All right.
And this way you don't make any mistake.
So that's the working directory for our script.
So that we can also run this as the Pi user.
That's going to be very important.
Then we have exec start like this is equal to an exec start is the command we want to run.
So a service is not necessarily going to run a Python script.
It's going to run any command that you want.
Now the command we want to run here is Python three project.
Step four dot p y.
But for this we are going to use the absolute path.
So we're going to use the absolute path for the file name here but also for the command.
And you might think what's the absolute path for Python three.
It's already a it's just a comment right.
But even comments have an absolute path.
And you can find that with the command which.
So you do which Python three.
And you see that when you run Python three.
Actually what's running is slash user slash bin slash Python three.
All right.
So this is kind of an alias for that one.
So I'm going to copy again and paste it there with Ctrl shift and C and Ctrl shift and V.
All right.
So error in Python three.
And then what.
That's the same path here for the Python program that I'm going to copy here.
And then slash and the name of the file is project step four dot p y.
Make sure you don't make any typo.
If you want, you can actually just copy that and run it inside the terminal and see if it's going to
work.
And you see it's working.
All right.
So you can be sure that you have the correct command.
That's very important.
Good.
Now I go back to a new line.
I have one more thing here.
User equal pi.
Okay.
So pi is pi because we are here connected as the user pi.
So that's the user you want to run the script as.
Right.
So if you used something different than pi you need to also replace the user here.
And we are almost done.
So finally this one last thing is going to be install like that and wanted by okay.
So be uppercase.
And we're going to write the same thing here multi That's user dot target.
All right.
So it's quite a lot of new things.
Once again I'm not going to explain everything.
That's something you can search on your own after the course.
But now the file is finished.
So I'm going to do Ctrl S and then Ctrl x.
And I have my new file that's going to be saved inside inside here.
And I'm going to directly create a service for the second Python program.
So that's going to be the project.
Step six.
Here we have run the project step four.
And we want the project step six to do this is going to be much simpler because I already created a
service is going to be almost the same.
So I can just copy it.
To copy it I'm going to also need to do sudo because I'm not in my home directory.
So sudo cp project take photos service.
That's the one I want to copy from.
And the new file is going to be for example project Web server dot service.
So that's the name for my second service.
Then I do sudo nano project web server.
Okay, that's the same one.
And what I will do is modify the description first because it's not accurate.
So this is going to be program actually uses a web server that displays last photo.
It's really not important.
So unit is the same description.
We can change it after it's going to be the same.
And then the working directory is the same because the Python file is still in that one.
But the exact start is going to be different only with the file name here you're going to be project
step six.
Okay.
So that's the only thing we need to change is the exact start.
So we don't run the same script.
All right.
The rest is the same.
Now let's do control S and X, and we have the two services.
All right.
So now that we have created those files we will be able to start and stop the service.
So make sure that you don't have anything running.
Okay.
Make sure that you are not running those files anywhere else on your Raspberry Pi.
Then let's go back to the home directory for example.
Let's do clear.
And now how to start the service.
We can do sudo systemctl.
Okay.
So we have the systemctl command with start and then the name of the service.
So for example project and then take photos.
So I can press tab here.
It should be found.
Okay.
So if it's correctly placed in the right folder with the right name and everything we should find it
here.
If I press enter you see that we go back to a new line.
Nothing happens.
But the program was actually started And you can check that.
For example, the easy check is just to put your hand in front of the Pir sensor and you should see
the LED should be turned on, and then you can put your hand for five seconds.
And you will also see new photos in the photos.
Final project okay, a new log file and you will also receive an email.
I can start my second program with pseudo system CTL start project with a web server.
And this is going to start the second Python program, this one.
How to test it.
Well you just go to the URL here and you see it's working okay.
So note I didn't run any Python script directly.
I just started the services.
Now if you have an error.
So for example if you cannot see the LED, if you cannot see new photos, if you cannot see that you
can use the journal Settle.
So you don't need sudo for that.
And then dash u and the name of the service for example that one.
I press enter and well that's actually not from today.
But the last time I, I tried this I had some error.
Okay.
So if you have some error logs then.
So I press Q here or ctrl C to exit.
So if you have some errors and you are not sure what you can use this Journalctl u and then the name
of the service.
Okay.
But if it's working fine you will not see anything in this file.
It's just for you to to know.
So let's, uh, clear that.
And now, while those programs are running in the background as services, how to stop them, you can
do sudo systemctl.
And we had the start.
So we have stop project take photos and then we do the same with project web server Seven and they are
stopped.
And you can see if I refresh this page here.
Now, you see it's not working anymore.
And if you put your hand in front of the Pir sensor, you will not see the LED anymore.
So you can see that the scripts have been stopped.
And so you can do start and stop just like that.
Great.
And now you might think that that's great.
Instead of starting the Python script directly, we can start them with the service.
But it's still the same as before.
And so yes, but now what we can do is we can enable those services so that they will be started automatically
on boot.
So that's why we create a service.
And before we enable the service there is a command you can use.
So sudo systemctl it's called list dash unit dash files.
Let's run that and you can see.
So we will have a lot of stuff.
And you can see the kind of file and then the state.
So we have state and presets.
What's going to be interesting for us is state and you can see some stuff is disabled, some stuff is
enabled.
Okay.
If I press enter I can scroll here.
And I'm just going to do Ctrl C and I'm going to run the command again.
But this time I can just do pipe.
So we've done this before in this course.
If you do pipe, basically the whole content of the left is going to be on the right.
And you can for example, do grep to filter the content.
And I'm going to do grep project.
So it's going to filter and only print the lines that correspond to this here on the right.
So that's quite useful.
And we can find our two services.
So you see with this you can find the services.
And then you see what's interesting for us is that first column here so disabled and disabled.
This means that if you start the Raspberry Pi again nothing's going to happen.
But now you can enable them.
So sudo systemctl enable.
Okay so the commands are quite simple.
You have start and stop to directly start and stop them right away.
And you have enabled to well, enable the service to be started on the next boot and every other boot
and let's do project take photos service.
You see it's going to create a symlink somewhere.
And now if I do this again you see that this one is enabled okay I'm going to do the same thing.
So I'm going to do enable project with web server.
And now they are enabled okay.
So what I recommend you is when you create a new system like that, you can use the start and stop just
to make sure that everything is working.
When everything is working, then you can enable them and how to test that.
Well I'm going to do a sudo reboot.
So let's reboot the Raspberry Pi.
And well as we reboot the Raspberry Pi, we also lose the connection to VNC because I was connected
with VNC and now you can already see actually if it's working because the LED should turn on if you
put your hand close to the Pir sensor.
Okay.
We didn't even connect to the Raspberry Pi, but if you see this, it means that the program is already
running on boot, so you don't need to connect to the Raspberry Pi to start it.
And then also I just opened a web browser and note that I opened this web browser here on my windows
computer.
So this computer is connected to the same network as the Raspberry Pi.
It's very important.
I could have opened that also from my smartphone if I wanted to, and I just typed the IP address of
the Raspberry Pi and then the port, and then slash check photos and you see we have no photo available.
So it's actually working.
You see, I can get access to that web page, well, basically from any web browser that's in the network.
And then if I take a photo.
So let's refresh.
You see, we have one new photo.
So I just took a photo.
Well, that's just my hand.
I was doing some movement.
And you see also the path to the photo.
So it's correctly working.
And we didn't even have to access the Raspberry Pi ourselves.
So this is a quite powerful functionality.
You can develop your application and then you create a service.
And then it's just going to run when you boot the Raspberry Pi.
Now let's connect again to VNC and let's get back to the Raspberry Pi.
So I am back here and I'm just going to show you one last thing is how to disable the services.
Because now that you have created the services and that they are enabled, they will run every time
you boot your Raspberry Pi.
So that's nice.
But what if you want to stop that?
Well I'm just going to show you this to conclude the project here.
So first of all let's do again sudo systemctl with list unit Files.
And let's do pipe grep to sort and with the project.
And we see that it's enabled.
So I can do sudo systemctl and you probably guessed the command.
So if we have enable to enable something we have disabled okay.
Quite easy to disable project take photo.
You see it's removing something here.
And I can do the same with project web server.
Okay.
And now if I list again you see those two are disabled.
And very important note they are disabled for the next boot okay.
Which means that they are actually still running.
If you check they are still running.
So if you want to stop them you will need to do sudo systemctl stop okay.
When you enable disable it's not starting or stopping the program, it's just enabling or disabling
the run of the program for the next boot.
All right.
So that's a very important difference.