Start up program while booting your raspi



A common problem when you use your Raspberry Pi to do applications that you want to run automatically when boots up, is the lack of experience with linux and into its initialization system. For those who are starting with Raspberry Pi, or for those who just don’t really know how  to get your application up and running since boot.
I’m gonna explain a little bit how Linux initializes, because I love knowing what I’m working with, and it’s going to help those people who are not just looking for the solution. If you want an answer it would be better to first understand the question. There is also a quick solution and you are free to skip this part (which I enjoy the most ;D) and go straight to the answer to this problem, you can eventually come back and view it later.
Ok so let’s go. I don’t wanna hit you with heavy stuff so I’ll try to be clear. When the Raspberry Pi is powered up the first thing that starts working is the GPU core. The GPU core is responsible for the first booting steps and it uses some kind of internal firmware to start accesing the SD card slot. As you probably know, the SD card is formatted in Fat32 (at least one partition) and should contains files that are used from the GPU core to boot the system.
The bootcode.bin file contains code to load elf binaries. It loads the start.elf file and it resets the ARM core. The ARM processor starts execution from the loaded kernel starting address.
Afterwards,  The ARM loads kernel.img (or whatever kernel you’ve defined in /boot/config.txt). When this file is loaded the Kernel is ready to work and it starts by setting up a few things (maybe more than a few) where one of the last processes to be set is the init process. And here is where we want to do something!
I’m working with Raspbian, a free operating system based on Debian optimized for the Raspberry Pi. And now I’m going to show you where you have to go and what exactly you have to add and change.
First you need to have root privileges. So all the following code lines are going to have the prefix sudo. Now you have to write a script. (just kidding… don’t quit) With this script you are going to take control of the program that you are going to launch. You will be able to start your program, stop it and see its status. Also you are going to be able to declare in the script wich runlevel it has. An interesting post about how runlevels work in System V is here.
So by now you may be wondering if you have to write this script that has to do with things you are still learning (or not maybe you are Linus Torvalds, in that case please be gentle). The answer is no, the template is also provided here.
You only have to follow the instructions to edit the script (just a few things). If you don’t know anything about scripts and you are about to run, I just recommend you to:
  1. In dir just write the working directory where your program is located.
  2. In user just write root
  3. In cmd you have to specify what command is going to be called. For example if you have a program that uses a make file you probably want to put “make run”.
This script sets up your deamon, and now you have to move it to the right directory:
sudo mv /programName /etc/init.d/
Give it execution privileges:
sudo chmod +x /etc/init.d/programName
And update the rc.d file:
sudo update-rc.d filename defaults
Now when you boot up your Raspberry Pi it will be running your process. You can check it by doing:
sudo top
And look if the daemon is running. You can also watch it status by doing:
sudo /etc/init.d/programName status 
If you want to stop it just:
sudo /etc/init.d/programName stop 
If you want to start it just:
sudo /etc/init.d/programName start 
If you want to restart it just:
sudo /etc/init.d/programName restart 
I recommend you to dive into the script you just have copied and try to understand what it is really doing inside.
REFERENCES:

No hay comentarios:

Publicar un comentario