I have a python script that takes sensor data from a DHT11 and inputs the data into MySQL. This file when ran in the terminal on my Raspberry Pi returns the right results:
python3 ./Data
In which will return successfully upload the data and print something along the lines of:
Writing to Sensor...
Write Complete
1623911938828.7395
[19.0, 65.0]
I have used sudo crontab -eand edited the bottom of the file to */1 * * * * python3 /home/pi/Data.py >> /home/pi/data.log 2> &1. The issue is when this runs there are no new data entries into MySQL, but if I run the script outside of the crontab, it will successfully upload the data to the table.
The Cron.log file looks normal to me:
Jun 17 16:25:58 raspberrypi cron[364]: (CRON) INFO (pidfile fd = 3)
Jun 17 16:25:58 raspberrypi cron[364]: (CRON) INFO (Running @reboot jobs)
Jun 17 16:26:01 raspberrypi CRON[615]: (root) CMD (pi /home/pi/Data.py >> /home/pi/data.log 2> &1)
Jun 17 16:26:43 raspberrypi systemd-timesyncd[322]: Synchronized to time server for the first time [2606:4700:f1::123]:123 (2.debian.pool.ntp.org).
Jun 17 16:27:16 raspberrypi CRON[1351]: (root) CMD (pi /home/pi/Data.py >> /home/pi/data.log 2> &1)
Jun 17 16:28:01 raspberrypi CRON[1355]: (root) CMD (pi /home/pi/Data.py >> /home/pi/data.log 2> &1)
Jun 17 16:29:01 raspberrypi CRON[1359]: (root) CMD (pi /home/pi/Data.py >> /home/pi/data.log 2> &1)
Jun 17 16:30:01 raspberrypi CRON[1363]: (root) CMD (pi /home/pi/Data.py >> /home/pi/data.log 2> &1)
Jun 17 16:31:01 raspberrypi CRON[1367]: (root) CMD (pi /home/pi/Data.py >> /home/pi/data.log 2> &1)
Jun 17 16:32:01 raspberrypi CRON[1428]: (root) CMD (pi /home/pi/Data.py >> /home/pi/data.log 2> &1)
Jun 17 16:33:01 raspberrypi CRON[1445]: (root) CMD (pi /home/pi/Data.py >> /home/pi/data.log 2> &1)
Jun 17 16:34:01 raspberrypi CRON[1492]: (root) CMD (pi /home/pi/Data.py >> /home/pi/data.log 2> &1)
Jun 17 16:34:26 raspberrypi crontab[1509]: (root) BEGIN EDIT (root)
Jun 17 16:34:50 raspberrypi crontab[1509]: (root) REPLACE (root)
Jun 17 16:34:50 raspberrypi crontab[1509]: (root) END EDIT (root)
Jun 17 16:35:01 raspberrypi cron[364]: (root) RELOAD (crontabs/root)
Jun 17 16:35:01 raspberrypi CRON[1523]: (root) CMD (python3 /home/pi/Data.py >> /home/pi/data.log 2> &1)
Jun 17 16:36:01 raspberrypi CRON[1549]: (root) CMD (python3 /home/pi/Data.py >> /home/pi/data.log 2> &1)
Jun 17 16:37:01 raspberrypi CRON[1573]: (root) CMD (python3 /home/pi/Data.py >> /home/pi/data.log 2> &1)
Jun 17 16:38:01 raspberrypi CRON[1581]: (root) CMD (python3 /home/pi/Data.py >> /home/pi/data.log 2> &1)
Jun 17 16:39:01 raspberrypi CRON[1594]: (root) CMD ( [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
Jun 17 16:39:01 raspberrypi CRON[1595]: (root) CMD (python3 /home/pi/Data.py >> /home/pi/data.log 2> &1)
Jun 17 16:40:01 raspberrypi CRON[1652]: (root) CMD (python3 /home/pi/Data.py >> /home/pi/data.log 2> &1)
Jun 17 16:41:01 raspberrypi CRON[1668]: (root) CMD (python3 /home/pi/Data.py >> /home/pi/data.log 2> &1)
Jun 17 16:41:22 raspberrypi crontab[1680]: (root) BEGIN EDIT (root)
Jun 17 16:42:01 raspberrypi CRON[1694]: (root) CMD (python3 /home/pi/Data.py >> /home/pi/data.log 2> &1)
Jun 17 16:43:01 raspberrypi CRON[1701]: (root) CMD (python3 /home/pi/Data.py >> /home/pi/data.log 2> &1)
Jun 17 16:44:01 raspberrypi CRON[1719]: (root) CMD (python3 /home/pi/Data.py >> /home/pi/data.log 2> &1)
Further, the file has been made executable -rwxr-xr-x 1 pi pi 1.1K Jun 17 13:19 Data.py . What could be the issue?
data.log? How do you authenticate against MySQL? Does your script also run as "real" root (sudo su -)?