The script is a Python script and works in a virtual environment. Made a shell script "launch.sh" that looks like this:
cd /home/pi/test_iot
. bin/activate
python3 rtl433_to_mqtt.py
If I run this from the console it works but not from cron.
Running "sudo crontab -e" and then added this line:
@reboot sh /home/pi/launch.sh >/home/pi/logs/cronlog 2>&1
I get this error log:
Traceback (most recent call last):
File "/home/pi/test_iot/rtl433_to_mqtt.py", line 120, in <module>
File "/home/pi/test_iot/lib/python3.11/site-packages/paho/mqtt/client.py", line 914, in connect
return self.reconnect()
^^^^^^^^^^^^^^^^
File "/home/pi/test_iot/lib/python3.11/site-packages/paho/mqtt/client.py", line 1044, in reconnect
sock = self._create_socket_connection()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/pi/test_iot/lib/python3.11/site-packages/paho/mqtt/client.py", line 3685, in _create_socket_connection
return socket.create_connection(addr, timeout=self._connect_timeout, source_address=source)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/socket.py", line 851, in create_connection
raise exceptions[0]
File "/usr/lib/python3.11/socket.py", line 836, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
I have no clue what's happening here so I hope someone here can see what's wrong.
It's hard to tell by the information provided. My guess is that the cron-job is firing before any network facilities are available. Instead of using
@rebootfrom cron, try a systemd service running afternetworkingis available.