lwhistler
Posts: 16
Joined: Sat Jul 16, 2016 10:37 pm

Auto run python script when Raspberry booted up.

Sun Apr 22, 2018 4:23 pm

What is the best way to run a python script when the computer is turned on? When I Googled it I came across some solutions including the one below.


Crontab

Code: Select all

@reboot /home/len/Desktop/3d.py


  • It works but it was way down the list of answers and only had 1 like.
  • It also was the simplest to implement.
  • Is there anything I should be aware of when I'm using the crontab @reboot method?

alphanumeric
Posts: 2547
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Auto run python script when Raspberry booted up.

Sun Apr 22, 2018 7:00 pm

It's how I do it. I forget why I have the & at the end though? It's been a long time since I looked up how to do it.
run sudo crontab -e
add
@reboot python3 /home/pi/myfile.py &

pcmanbob
Posts: 9462
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Auto run python script when Raspberry booted up.

Sun Apr 22, 2018 7:08 pm

When placing & at the end of a line like that it makes bash start program in the background, so you can continue to use the shell and do not have to wait until the script is finished.

But it is not required in cron because programs are already run in the background.

running a python program like that is fine if you do have any screen output from the program.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

alphanumeric
Posts: 2547
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Auto run python script when Raspberry booted up.

Sun Apr 22, 2018 7:23 pm

Great, thanks for that info. Was just looking to see if I had saved a link to where I first looked that up. The Pi's I'm dong that on run headless with no monitor, keyboard or mouse. And boot to command line. Info / output is shown on a Sense hat LED matrix

Return to “Beginners”