I have a python script which makes use of the standard RPi I/O's where I am detecting multiple inputs and storing values to a database. The script runs without exiting until the Pi is powered off.
The scripts write values to a database which are accessed by a Django application on the same machine. Therefore I would know whether inputs are being detecting by navigating to my localhost to check what is being displayed on my Django app.
Script located at /home/pi/Desktop/myscript.py
I want to be able to run this script whenever the raspberry pi is booted. I have tried several methods shown here so far but none have worked.
I have also followed the instructions here, but the script does not seem to execute on startup.
As per instructions:
sudo nano /etc/xdg/lxsession/LXDE-pi/autostart @lxterminal -e "/home/pi/myscript.sh"
and I create the myscript.sh file:
cd /home/pi sudo nano myscript.sh sudo /usr/bin/python3 /home/pi/Desktop/myscript.py
The above method does not run the script for me, (or at least inputs are not detected)
I also tried including the script in my cron file as below:
Edit the file:
sudo crontab -e
Add line to file (here a python script):
@reboot python3 /home/pi/Desktop/myscript.py &
This does not work either.
Lastly I have tried the below method:
All you need to do here is put ./myscript in the rc.local text file. If it's in python, put python myscript.py.
cd /etc sudo nano rc.local
I then write:
python3 /home/pi/Desktop/myscript.py &
I have read here, however, that:
hings initiated from rc.local should either execute and finish quickly (a few seconds or less) or else fork to the background.
None of the above methods have worked, meaning that no inputs were detected, and I can't seem to understand why? Any help would be appreciated.
On trying the below answer I get the following in my journal:
-- Logs begin at Thu 2016-11-03 18:16:43 CET, end at Mon 2018-09-03 06:10:07 CEST. -- Sep 03 06:10:07 raspberrypi systemd1: Started My script.
I can confirm that the script is actually running, since I am printing database values to a text file using:
#At the end of the shift we are writing db to csv file before deleting table data c.execute("SELECT * FROM LineOEE03") with open('/home/pi/Desktop/OEE/Logs/output '+moment+'.csv', 'w', newline='') as csv_file: csv_writer = csv.writer(csv_file) csv_writer.writerow([i for i in c.description]) csv_writer.writerows(c)
Indeed, a text file was created at the desired directory with the current date and time.