I have a simple python file(no GUI) that I'd like it to run in the background at start-up of raspberry pi 3b. I checked out this post Execute script on start-up But it didn't specify what to do with python file though. Python script:

import time
import pygame
while True:

The complete file is at: https://github.com/adafruit/Adafruit_Python_MPR121/blob/master/examples/playtest.py However I'm unable to make it work, so just used a simpler version instead. Appreciate the help.

  • posted the wrong thread location. That script wasn't the one I was looking at. – June Wang Jul 13 '18 at 2:35
  • DO NOT put ANYTHING in .bashrc - this is extremely poor practice as it will run EVERY time you execute a non-login shell. It is intended to initialise the shell. The other suggestions are obsolete SysV code. – Milliways Jul 13 '18 at 3:04
  • That's why I'm asking here for another solution. – June Wang Jul 13 '18 at 3:17
  • There are Hundreds (if not thousands) of similar questions on this site. Have you looked? Unfortunately many wrong or obsolete - even then IT DEPENDS on the script - the question is unanswerable without. – Milliways Jul 13 '18 at 3:37
  • Just added script, and yes I have looked. The variety of solutions confused me. – June Wang Jul 13 '18 at 3:39

A systemd unit will do the job. Create a new one with:

rpi ~$ sudo systemctl --force --full edit myPythonScript.service

In the editor insert these statements, save it and quit the editor:

Description=running my python script

User=pi     # or must it run as root? Then omit this line
ExecStart=/home/pi/myPythonScript.py   # or where your program is


Enable the new service:

rpi ~$ sudo systemctl enable myPythonScript.service


Check with:

rpi ~$ systemctl status myPythonScript.service
rpi ~$ systemctl cat myPythonScript.service

Edit again with:

rpi ~$ sudo systemctl --full edit myPythonScript.service

I don't know if it runs on the first attempt. There may be some edge conditions I don't know, something like user rights, environment etc. It is no problem to configure these dependencies. The problem is to find them ;-) Give me a comment under this answer if it doesn't run at first go.

| improve this answer | |

I normally refrain from answering these questions, because there is so many things to consider (also I generally don't answer python questions, due to lack of expertise).

When I want to run something at boot I write a systemd service (but this is complex). For simple tasks I use crontab.

The Foundation has a tutorial Scheduling tasks with cron

Some additional points to consider

  • DO NOT use sudo
  • Use Full paths on ALL files including python - even though the Foundation omits this
  • DO NOT run programs which output to terminal
  • Be wary of anything that includes delays
  • Use proper shebang on script files e.g. #! /usr/bin/env python3

Many recommend rc.local - although this works (sometimes) it is an obsolete SysV technique with hidden traps because systemd runs startup asynchronously.

| improve this answer | |
  • #! /usr/bin/env python3 myfile.py shoud be added at the beginning of the python file? – June Wang Jul 13 '18 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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