0

Not sure where the issue is but my code doesnt run correcrtly when its in a cron.

This is in my cron.d:

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
@reboot pi /home/pi/scripts/airpi/venv38/bin/python /home/pi/scripts/airpi/airpi.py  >> /home/pi/scripts/airpi/logs/`date +\%Y\%m\%d\%H\%M`_airpi.txt 2>&1 &

Here is what I see in the logs when it runs:

['/home/pi/scripts/airpi', '/usr/local/lib/python38.zip', '/usr/local/lib/python3.8', '/usr/local/lib/python3.8/lib-dynload', '/home/pi/scripts/airpi/venv38/lib/python3.8/site-packages']
Traceback (most recent call last):
  File "/home/pi/scripts/airpi/venv38/lib/python3.8/site-packages/urllib3/connection.py", line 159, in _new_conn
    conn = connection.create_connection(
  File "/home/pi/scripts/airpi/venv38/lib/python3.8/site-packages/urllib3/util/connection.py", line 61, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "/usr/local/lib/python3.8/socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Temporary failure in name resolution

During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "/home/pi/scripts/airpi/venv38/lib/python3.8/site-packages/urllib3/connectionpool.py", line 670, in urlopen
        httplib_response = self._make_request(
      File "/home/pi/scripts/airpi/venv38/lib/python3.8/site-packages/urllib3/connectionpool.py", line 381, in _make_request
        self._validate_conn(conn)
      File "/home/pi/scripts/airpi/venv38/lib/python3.8/site-packages/urllib3/connectionpool.py", line 978, in _validate_conn
        conn.connect()
      File "/home/pi/scripts/airpi/venv38/lib/python3.8/site-packages/urllib3/connection.py", line 309, in connect
        conn = self._new_conn()
      File "/home/pi/scripts/airpi/venv38/lib/python3.8/site-packages/urllib3/connection.py", line 171, in _new_conn
        raise NewConnectionError(
    urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x75759298>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution

Now when I run it manually using the same command I have in the cron as user pi it runs correctly:

pi@raspberrypi:~/scripts/airpi $ /home/pi/scripts/airpi/venv38/bin/python /home/pi/scripts/airpi/airpi.py
['/home/pi/scripts/airpi', '/usr/local/lib/python38.zip', '/usr/local/lib/python3.8', '/usr/local/lib/python3.8/lib-dynload', '/home/pi/scripts/airpi/venv38/lib/python3.8/site-packages']
PM2.5 = 14.0
PM10 = 21.8
PM2.5 = 13.9

Here's the code: https://github.com/chowpay/airpi

2
  • 2
    What happens if you add a delay of 20-30 seconds? Gut feel is that the task is running before networking is fully up. I do not think that setting the 'wait for networking' in raspi-config would help in this case.
    – user115418
    Nov 3, 2020 at 21:08
  • I think you're right so I added a sleep But now I dont think my cron.d is working at all: SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin @reboot sleep 60 && pi /home/pi/scripts/airpi/venv38/bin/python /home/pi/scripts/airpi/airpi.py >> /home/pi/scripts/airpi/logs/date +\%Y\%m\%d\%H\%M_airpi.txt 2>&1 &
    – chowpay
    Nov 3, 2020 at 21:47

1 Answer 1

0

@Andyroo is correct the issue I had was a delay in the network coming up before the script started to run. But I could not get the syntax right. Finally figured it out:

Here is the correct format to add sleep for cron.d:

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

@reboot pi sleep 60 && /home/pi/scripts/airpi/venv38/bin/python /home/pi/scripts/airpi/airpi.p

If using crontab -e

@reboot sleep 60; /home/pi/scripts/airpi/venv38/bin/python /home/pi/scripts/airpi/airpi.py

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.