如何在Raspbian虚拟环境中让脚本自动启动?

问题描述 投票:0回答:1

该脚本是Python脚本,在虚拟环境中工作。 制作了一个 shell 脚本“launch.sh”,如下所示:

cd /home/pi/test_iot
. bin/activate
python3 rtl433_to_mqtt.py

如果我从控制台运行它,它可以工作,但不能从 cron 运行。 运行“sudo crontab -e”,然后添加以下行:

@reboot sh /home/pi/launch.sh >/home/pi/logs/cronlog 2>&1

我收到此错误日志:

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

我不知道这里发生了什么,所以我希望这里有人能看到出了什么问题。

linux virtualenv raspbian autostart
1个回答
0
投票

根据提供的信息很难判断。我的猜测是,在任何网络设施可用之前,cron 作业就会被触发。不要使用 cron 中的

@reboot
,而是尝试在
networking
可用后运行 systemd 服务。

© www.soinside.com 2019 - 2024. All rights reserved.