You can do this quite easily by adding a line to your crontab
file. Do this:
From your $ shell prompt, enter man crontab
. Read what it says. When you finish, dismiss the man page be entering q
, and then enter crontab -e
on the command line.
The nano
editor will start, and show you a "default" crontab in which each line is commented - it begins with "#". Using nano
, move the insertion point to the first new line at the end of the file. Enter this:
@reboot /usr/bin/python3 ~/somefolder/somefile.py &
Where ~/ is your home directory (probably /home/pi), "somefolder" is a folder in your home directory containing the Python script you want to run, and "somefile.py" is your Python script. The &
lets your Python script run "in the background", and doesn't hold things up.
After you've added this line, use nano
's Write
(^O
) to save the file, and accept whatever filename it suggests. Then exit nano
using ^X
. You'll get a confirmation message that your crontab file has been updated.
Reboot your pi, and your Python script will execute; and it will execute each time your pi reboots.
systemd
. – Ingo Apr 20 '18 at 12:40systemd
. I don't make it for you. Look at the internet. There are many examples. You can also look atman systemd.unit
. If you get stuck with a specific problem on this I can help you. To give you an idea you can look at this example for a systemd service. And please take the Tour. – Ingo Apr 20 '18 at 12:58