This article has been transferred to a new blog, please click here ( https://linzichun.com/posts/autostart_application_linux/) to view the updated article.
Thank you !!!
—————————————–
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
Hello, I have just received a RASPBERRY PI with SD CARD 16 GB. SD CARD has its autostart. Before I changed something:
ps aux | grep test.sh
and I see 2 lines. I select first line. Then,
kill 674 etc.
ps aux | grep project.py
I did same things.
Then, I open project folder, and I changed any code in any .py file. Then, how can I auto start all over again. Do I have to ? Those who gave me this raspberry pi followed the instruction to auto start from this link: https://www.wikihow.com/Execute-a-Script-at-Startup-on-the-Raspberry-Pi
Please help me ?
LikeLike