I've been all over the internet looking for a solution for this but everybody seems to have different answers and none of them work. I have a python script that emits audio through the headphone jack when ran, and I want it to run on boot without having to connect to a monitor and run it myself. How would I make /home/pi/Desktop/sample.py run on boot?
-
5Given the fact that you say: "I've been all over the internet looking for a solution ... and none of them work." What can we add to this if you have tried them all, and why would anyone take time to post an answer only to be told oh that didn't work. You need to edit your question and say exactly what you tried, and how they failed - including any code you changed/added and any error messages. Do not post these items in the comments - edit them into your question. Also, post text not photos of your screen.– Steve RobillardJun 22, 2021 at 21:50
-
there are many answers on the web if you realize that the question is not related to the Raspberry Pi ... it is a linux question ... you actually posted this in the wrong place– jsotolaJun 22, 2021 at 21:55
-
@jsotola: I see your point, but I disagree with your assessment.– SeamusJun 22, 2021 at 22:17
-
Did you visit this corner of the internet blog.usedbytes.com/2019/11/run-at-startup-without-rc.local ?– ukBazJun 23, 2021 at 16:48
2 Answers
You should use a systemd service. These services are used to start programs. You will find many examples how to do it at tag systemd-startup.
-
ha ha ha :) here we go with that
systemd
stuff again LOL . Happy to see you're back :)– SeamusJun 22, 2021 at 22:09 -
-
Thank you for that :) I do now use
systemd
with some regularity... when I need to stopcron
withsudo systemctl stop cron.service
. So - I can learn new tricks - I am modern lol– SeamusJun 22, 2021 at 22:40
Have you tried cron
?
Assuming you can run the script from the terminal, as user pi
(i.e. No sudo
required), and it runs correctly:
From the terminal (CLI; don't enter the $
in column 1 - it's the shell prompt):
$ crontab -e
You may be prompted to choose an editor for your
crontab
;nano
orpico
are reasonable choices if you're not familiar with the other editors.
In the editor, add this one line:
@reboot /bin/sleep 10; /usr/bin/python /home/pi/Desktop/sample.py >> sample_py_log.txt 2>&1
Let's break it down:
@reboot
- runs the designated program(s) whencron
daemon is started/bin/sleep 10
- wait/sleep
for 10 seconds before resuming this command; this allows time for any other resources in your script to be started (e.g. sound drivers, etc);
- tellssh
there's another command coming/usr/bin/python
- you may need/usr/bin/python3
instead - depending upon how your system is configured/home/pi/Desktop/sample.py
- full path to your script>> sample_py_log.txt
- re-directs output (stdout
) of script to the named file2>&1
- re-directsstderr
(2) tostdout
so that you "capture" any error messages thrown by your script. This is necessary becausecron
was not designed to be "interactive" w/ users.
Finally, save your edited crontab
, exit the editor, and reboot your RPi to test. Let us know if you have questions.