0

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?

2
  • 1
    Why are you using the root crontab? Do you run the script as root from the terminal?
    – joan
    Commented Jun 17, 2021 at 9:03
  • Are there new entries in data.log? How do you authenticate against MySQL? Does your script also run as "real" root (sudo su -)? Commented Jun 17, 2021 at 11:54

1 Answer 1

1

I think two changes may be needed: 2>&1 (no space), and /usr/bin/python3 instead of python3.

Also, @joan comment: don't use root crontab unless it is needed. Its primary benefit (IMO) is that it gets around potential complications of using sudo in a cron job.

2
  • What do I do to not use the root crontab? Sorry am quite new to this
    – Arch1234
    Commented Jun 18, 2021 at 1:54
  • @Arch1234: A couple of reasons I believe: 1. It's not considered good security practice, 2. It may cause unexpected results in some applications. Question for you: Did this fix your problem?
    – Seamus
    Commented Jun 18, 2021 at 5:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.