If I understand your issue, you want to start your Python script when your RPi starts (boots) - not when you log in... is that correct?
If so, then the crontab
is a proper way to do that. This would be easier to figure out if you'd edit your question to add the job (line) you put in your crontab
. But we'll give it a go, and create a "job" in crontab
from scratch.
Begin by opening your user crontab
from the command line of your terminal window:
crontab -e
Assuming that you're using Raspberry Pi OS, and you've not changed the defaults, your default editor (nano
) should open with a copy of your current crontab
file. I'll assume you're familiar enough with your editor to follow along...
Add a new line to the bottom of your editor window/screen with the following:
@reboot /path/to/script >> /home/pi/myscriptlog.txt 2>&1
What does this do?
When your machine boots up, this job in your crontab
will run (@reboot
)
The cron
daemon (service) will attempt to execute whatever file is located at /path/to/script
. (NOTE be sure this file is set to be executable; use chmod
to set the executable bits if necessary)
Since the cron
job cannot communicate with you through the terminal, we redirect (>>
) the output to a file in your home directory (here assumed to be /home/pi
).
The 2>&1
bit at the end of the job is another redirect, but this one tells cron
to redirect any error messages thrown by your script (i.e. stderr
= 2) into the stream with stdout
(i.e. stdout
= 1).
Try that, and let us know how it works - or, if this answer completely misses the point of your question, please let me know, and I'll delete the answer.