Reputation: 831
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.
Upvotes: 0
Views: 905
Reputation: 831
Both advices I got here worked perfect but I finally decided to use the service-version as I suspect it is more safe than waiting 60 seconds.
The service defined as:
[Unit]
Description=Temperature monitoring through RTL-SDR reciever
After=network-online.target
[Service]
Type=idle
ExecStart=/home/pi/launch.sh
Restart=always
[Install]
WantedBy=multi-user.target
The running script:
enter code here
#!bin/bash
cd /home/pi/test_iot
. bin/activate
#sleep 60# Not needed as we run it as a service that delays start until network online. When run as a cron-job it is needed
python3 rtl433_to_mqtt.py
Upvotes: 0
Reputation: 15239
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 @reboot
from cron, try a systemd service running after networking
is available.
Upvotes: 0