Sometimes, we need to start a program when pi boot up, there are several ways to achieve it, I introduce two ways to configure it.
First way:Create a .desktop file
1.Create autostart folder
$ cd /home/pi/.config $ mkdir autostart $ cd autostart
2.Write desktop file
$ sudo nano programname.desktop
A new window will open, type following command:
[Desktop Entry] Name=programname Exec=lxterminal –e “home/pi/yourprogram” Type=Application
After typing, press “Ctrl + O” to save and press “Ctrl + X” to quit nano.
After reboot, the program will autostart when raspberry pi start up.
Note:
[Desktop Entry] is always in the first line.
Name: Specific name of the application.
Type: This specification defines 3 types of desktop entries: Application (type 1), Link (type 2) and Directory (type 3).
Exec: Program to execute, possibly with arguments.
lxterminal –e “home/pi/yourprogram” means you want to use LXTerminal open the program that stored in“home/pi/yourprogram”, yourprogram is an executable file.
If you don’t want to autostart the program:
Go to Root LXTerminal,
$ cd /home/pi/.config/autostart $ sudo rm yourprogram.desktop
the desktop file will be deleted.
Second way:Create a script
1.Write a script to start the program
Create a new file in /home/pi/: autostart.sh
open it:
$ sudo nano /home/pi/autostart.sh
and write something:
/home/pi/yourprogram
2.configure to autostart the script
$ sudo nano /etc/rc.local
add the following lines before “eixt 0”
/home/pi/autostart.sh
3.
sudo chmod +x /home/pi/autostart.sh
After reboot,the program will autostart when raspberry pi start up.
You can change the program file path, no need to put in /home/pi/.
^ . ^
Great article; very useful.
LikeLike
Quick question: how do you make the pretty grayed boxes that hold the code? I need to learn to do the same on my website.
LikeLike
Sorry for late reply…using code format when you editting
LikeLike
Also, you misspelled “exit 0” in your article.
LikeLike