0

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?

4
  • 5
    Given 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. Jun 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
    – jsotola
    Jun 22, 2021 at 21:55
  • @jsotola: I see your point, but I disagree with your assessment.
    – Seamus
    Jun 22, 2021 at 22:17
  • Did you visit this corner of the internet blog.usedbytes.com/2019/11/run-at-startup-without-rc.local ?
    – ukBaz
    Jun 23, 2021 at 16:48

2 Answers 2

3

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.

3
  • ha ha ha :) here we go with that systemd stuff again LOL . Happy to see you're back :)
    – Seamus
    Jun 22, 2021 at 22:09
  • @Seamus just so it won't be forgotten. But cron is also "upvoteable"
    – Ingo
    Jun 22, 2021 at 22:26
  • Thank you for that :) I do now use systemd with some regularity... when I need to stop cron with sudo systemctl stop cron.service . So - I can learn new tricks - I am modern lol
    – Seamus
    Jun 22, 2021 at 22:40
2

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 or pico 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) when cron 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)
  • ; - tells sh 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 file
  • 2>&1 - re-directs stderr (2) to stdout so that you "capture" any error messages thrown by your script. This is necessary because cron 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.

Your Answer

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

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