Start your script as service. First create a new service with:
rpi3 ~$ sudo systemctl edit --force --full rfid-reader.service
Insert this statements, save them and quit the editor:
[Unit]
Description=Setting up the RFID reader
Wants=sockets.target
After=sockets.target
[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi
ExecStart=/home/pi/script.py
[Install]
WantedBy=multi-user.target
Check the new service:
rpi3 ~$ systemctl status rfid-reader.service
● rfid-reader.service - Setting up the RFID reader
Loaded: loaded (/etc/systemd/system/rfid-reader.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Now you can enable and test your service:
rpi3 ~$ sudo systemctl enable rfid-reader.service
rpi3 ~$ sudo systemctl start rfid-reader.service
I cannot test it because I have no RFID reader. You are alone with this. Here are some commands that may help:
rpi3 ~$ systemctl status rfid-reader.service
rpi3 ~$ systemctl cat rfid-reader.service
rpi3 ~$ sudo systemctl restart rfid-reader.service
rpi3 ~$ sudo systemctl edit --full rfid-reader.service
One problem is that I cannot check out for you the target after
that /dev/ttyUSB0
is available. In the example above I use
sockets.target
. Don't know if this already initialised /dev/ttyUSB0
.
With
rpi3 ~$ systemctl
you will find what services and targets are active. Only look at the targets. Other candidates for Wants=
and After=
seem to be:
local-fs.target Local File Systems
sysinit.target System Initialization
basic.target Basic System
If sockets.target
does not work I would try other targets in this order.
/dev/ttyUSB0
does not exist at the time when you run your code ... run the code later in the boot sequesnce – jsotola Apr 20 '18 at 21:51