How to AutoStart a program in Raspberry Pi or Linux?

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/.
^ . ^

Advertisements

5 thoughts on “How to AutoStart a program in Raspberry Pi or Linux?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s