由crontab运行时从Python套接字中获取错误,但手动运行时不会



我有一个运行Python脚本的shell脚本,该shell脚本在启动时由crontab运行。每次运行时,我都让shell脚本将错误记录到日志文件中。手动运行时,Python脚本运行良好,没有错误,Python脚本运行Discord bots btw。然而,当我重新启动我的Raspberry Pi,并让crontab运行shell脚本时,我收到了以下错误:对于上下文,有3个Python脚本,每个脚本都命名为";main.py";在相应的文件夹中。如果你想看的话,下面是shell脚本:

#!/bin/bash
cd /home/pi/Desktop/logs
DATE="$(date "+%m%d%Y_%T")"
touch "log_file_$DATE"
LOGFILE=/home/pi/Desktop/logs/log_file_$DATE
(
echo "-------------$(date "+%m/%d/%Y %T") : Starting work----------------"
cd "/home/pi/Desktop/InviteBot-master/"
/home/pi/Python-3.8.5/python main.py &
cd "/home/pi/Desktop/CustomRoleBot-master"
/home/pi/Python-3.8.5/python main.py &
cd "/home/pi/Desktop/Private-channel-manager-master"
/home/pi/Python-3.8.5/python main.py &
) >& $LOGFILE
sleep 86400
echo "-------------$(date "+%m/%d/%Y %T") : Restarting----------------"
sudo reboot

这是完整的日志输出(为了显示它,我用星号替换了机器人令牌(

-------------11/04/2020 16:53:36 : Starting work----------------
Traceback (most recent call last):
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 964, in _create_direct_connection
hosts = await asyncio.shield(self._resolve_host(
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 829, in _resolve_host
addrs = await 
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/resolver.py", line 29, in resolve
infos = await self._loop.getaddrinfo(
File "/home/pi/Python-3.8.5/Lib/asyncio/base_events.py", line 825, in getaddrinfo
return await self.run_in_executor(
File "/home/pi/Python-3.8.5/Lib/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/pi/Python-3.8.5/Lib/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
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "main.py", line 338, in <module>
client.run(TOKEN)
File "/home/pi/.local/lib/python3.8/site-packages/discord/client.py", line 678, in run
return future.result()
File "/home/pi/.local/lib/python3.8/site-packages/discord/client.py", line 658, in runner
await self.start(*args, **kwargs)
File "/home/pi/.local/lib/python3.8/site-packages/discord/client.py", line 621, in start
await self.login(*args, bot=bot)
File "/home/pi/.local/lib/python3.8/site-packages/discord/client.py", line 472, in login
await self.http.static_login(token.strip(), bot=bot)
File "/home/pi/.local/lib/python3.8/site-packages/discord/http.py", line 288, in static_login
data = await self.request(Route('GET', '/users/@me'))
File "/home/pi/.local/lib/python3.8/site-packages/discord/http.py", line 185, in request
async with self.__session.request(method, url, **kwargs) as r:
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/client.py", line 1012, in __aenter__
self._resp = await self._coro
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/client.py", line 480, in _request
conn = await self._connector.connect(
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 523, in connect
proto = await self._create_connection(req, traces, timeout)
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 858, in _create_connection
_, proto = await self._create_direct_connection(
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 971, in _create_direct_connection
raise ClientConnectorError(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host discord.com:443 ssl:default [Temporary failure in name resolution]
Traceback (most recent call last):
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 964, in _create_direct_connection
hosts = await asyncio.shield(self._resolve_host(
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 829, in _resolve_host
addrs = await 
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/resolver.py", line 29, in resolve
infos = await self._loop.getaddrinfo(
File "/home/pi/Python-3.8.5/Lib/asyncio/base_events.py", line 825, in getaddrinfo
return await self.run_in_executor(
File "/home/pi/Python-3.8.5/Lib/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/pi/Python-3.8.5/Lib/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
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "main.py", line 215, in <module>
client.run(TOKEN)
File "/home/pi/.local/lib/python3.8/site-packages/discord/client.py", line 678, in run
return future.result()
File "/home/pi/.local/lib/python3.8/site-packages/discord/client.py", line 658, in runner
await self.start(*args, **kwargs)
File "/home/pi/.local/lib/python3.8/site-packages/discord/client.py", line 621, in start
await self.login(*args, bot=bot)
File "/home/pi/.local/lib/python3.8/site-packages/discord/client.py", line 472, in login
await self.http.static_login(token.strip(), bot=bot)
File "/home/pi/.local/lib/python3.8/site-packages/discord/http.py", line 288, in static_login
data = await self.request(Route('GET', '/users/@me'))
File "/home/pi/.local/lib/python3.8/site-packages/discord/http.py", line 185, in request
async with self.__session.request(method, url, **kwargs) as r:
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/client.py", line 1012, in __aenter__
self._resp = await self._coro
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/client.py", line 480, in _request
conn = await self._connector.connect(
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 523, in connect
proto = await self._create_connection(req, traces, timeout)
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 858, in _create_connection
_, proto = await self._create_direct_connection(
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 971, in _create_direct_connection
raise ClientConnectorError(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host discord.com:443 ssl:default [Temporary failure in name resolution]
Traceback (most recent call last):
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 964, in _create_direct_connection
hosts = await asyncio.shield(self._resolve_host(
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 829, in _resolve_host
addrs = await 
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/resolver.py", line 29, in resolve
infos = await self._loop.getaddrinfo(
File "/home/pi/Python-3.8.5/Lib/asyncio/base_events.py", line 825, in getaddrinfo
return await self.run_in_executor(
File "/home/pi/Python-3.8.5/Lib/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/pi/Python-3.8.5/Lib/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
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "main.py", line 1134, in <module>
client.run('*****')
File "/home/pi/.local/lib/python3.8/site-packages/discord/client.py", line 678, in run
return future.result()
File "/home/pi/.local/lib/python3.8/site-packages/discord/client.py", line 658, in runner
await self.start(*args, **kwargs)
File "/home/pi/.local/lib/python3.8/site-packages/discord/client.py", line 621, in start
await self.login(*args, bot=bot)
File "/home/pi/.local/lib/python3.8/site-packages/discord/client.py", line 472, in login
await self.http.static_login(token.strip(), bot=bot)
File "/home/pi/.local/lib/python3.8/site-packages/discord/http.py", line 288, in static_login
data = await self.request(Route('GET', '/users/@me'))
File "/home/pi/.local/lib/python3.8/site-packages/discord/http.py", line 185, in request
async with self.__session.request(method, url, **kwargs) as r:
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/client.py", line 1012, in __aenter__
self._resp = await self._coro
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/client.py", line 480, in _request
conn = await self._connector.connect(
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 523, in connect
proto = await self._create_connection(req, traces, timeout)
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 858, in _create_connection
_, proto = await self._create_direct_connection(
File "/home/pi/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 971, in _create_direct_connection
raise ClientConnectorError(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host discord.com:443 ssl:default [Temporary failure in name resolution]

提前感谢您为我解决这个问题提供的任何帮助。

我发现我收到错误是因为我的网络还没有启动。因此,在执行python脚本之前,我通过添加以下代码更改了我的shell脚本:

while ! (ping -c 1 -W 1 1.2.3.4 | grep -q 'statistics'); do
echo "Waiting for 1.2.3.4 - network interface might be down..."
sleep 1
done

我从这里得到了建议:https://www.raspberrypi.org/forums/viewtopic.php?t=148716以及这里的代码:在bash 中继续之前,等待网络链接打开

最新更新