1

Trying to get the Raspberry pi to run python programs automatically on desktop once it has booted up.

Tried 5 different methods of trying to get python programs to automatically open on boot up into desktop without success.

Is there something one has to add to the python programs to activate or save them as a sudo nano sample.py.

Method 1:

I have tried adding the link to the rc.local folder

sudo python3 /home/pi/Desktop/GUI.py

No success.

Method 2:

I have tried adding sudo python3 /home/pi/Desktop/GUI.py

.bashrc

Soon has I open terminal these programs will open.

There is no errors at tall in the python programs.

Just following the methods described on a few forums the programs will not open in Desktop unless you open terminal then the program activates straight away for some reason.

Any advice most welcoming.

https://www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup/

2
  • The script will be running in the background (if it is running at all). It will not be attached to a keyboard or screen. Are you sure it's not running? Perhaps it just has nowhere to output any results.
    – joan
    Commented Dec 5, 2017 at 8:04
  • At the time I looked for the output and there was no indication at all that the program was running.
    – MSI
    Commented Feb 11, 2018 at 6:05

3 Answers 3

1

This is a common question that has already been answered in another thread. Essentially you need to set it up in the LXDE environment's autostart file.

Here is the link to that thread with full instructions:

Execute script on start-up

Good luck!

1

There is a difference between at boot and at login. This may be somewhat obscured in Raspbian if you use autologin, because right after booting finishes a login will automagically occur.

Booting is something the system does when started irregardless of whether anyone is going to log in afterward. Boot services are by default owned by the superuser (root), although this ownership is often switched to de-escalate privileges, making the service less of a security risk. The activities started at boot run in the background -- except for login consoles (and/or GUI logins, aka. "display managers"). Note the system can be configured to run something else in the foreground instead, but this is the norm for a multi-purpose desktop style system. Also note there does not have to be anything run in the foreground, which is the case for a headless system, with no physical HID (human interface device such as a keyboard or monitor).

"Foreground" is a little subjective. In one sense it refers to the perspective of a user. In another (compatible) sense, it refers to a relationship between a parent and child process -- a foreground process will block its parent until it completes or goes into the background.

Note that all processes have a parent, except for the first one, init, which is responsible for starting most processes at boot. On Raspbian init is systemd. If the parent of a process dies, any children which survive will be adopted by init.

Login is something a user does to start an individual session with the system. This may be anytime after the system has more or less finished booting.

I have tried adding the link to the rc.local folder

This is no good for what you want, which is to start something inside a login session. /etc/rc.local is a catch-all boot script intended to run things in the background (unless they can be completed very quickly).

I have tried adding sudo python3 /home/pi/Desktop/GUI.py

.bashrc

Soon has I open terminal these programs will open.

Yes because .bashrc is a configuration script for an interactive shell. The shell is the program which gives you a command-line prompt. From man bash (the INVOCATION section):

When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists.

When you open a GUI terminal, it starts a new (interactive, non-login) shell, which explains the behaviour you are experiencing. It is sometimes suggested to instead use .profile (or /etc/profile, or something in /etc/profile.d), since this is the shell configuration for an interactive login shell (a login shell is the shell first opened after you complete a text console based login). However, this is not a great idea since if you are using a GUI, there is never a login shell (although some desktop environments will run .profile to make up for this. It is probably a bad place to start anything GUI oriented since it applies to remote text logins (e.g., ssh) as well (although you could probe the context conditionally to deal with this).

With regard to running something just once at the start of a GUI login session, this is specific to the DE ("desktop environment") you are using. The current default on Raspbian is PIXEL, a spin-off of LXDE.

0

Try this:

crontab -e

Add this line:

@reboot sudo python path-to-your-python-script

If you are accessing network interfaces like Wi-Fi, try:

@reboot sleep 20 && sudo python path-to-your-python-script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.