0

I have a shell script that runs Python scripts and the shell script is run by a crontab upon startup. I had the shell script log the error into a logs file each time it is run. When run manually the Python scripts run fine and there are no errors, the Python scripts run Discord bots btw. However when I reboot my Raspberry Pi, and leave the crontab to run the shell script, I receive in error which is below: For context, there are 3 Python scripts, each one is named "main.py" in there respective folders. Here's the shell script if you want to look at it:

#!/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

and here's the full log output (I replaced the bot token with asterisks for the purpose of presenting it)

-------------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]

Thank you in advance for any help I can receive with this problem.

Sheepposu
  • 79
  • 9
  • The error appears to indicate that the system is unable to resolve a name to an IP number. You could try putting the IP number in directly, instead of the name, to see if that's indeed the problem. If it is, you should look into why your CLI does resolve the name but your script cannot. – Grismar Nov 04 '20 at 23:21
  • How should I go about finding the IP number that it is needing? – Sheepposu Nov 05 '20 at 01:56
  • One way would be to type `ping discord.com` from the same command line you run the scripts from manually without issue. It will tell you the IP number. – Grismar Nov 05 '20 at 02:49
  • I figured out that it was not working because the network on my Raspberry Pi was not yet up, I'll post an answer displaying what I did to my shell script to fix the problem – Sheepposu Nov 05 '20 at 05:06
  • If the question is no longer relevant because the problem cannot be reproduced (simply turning it on doesn't have much news value :)), please close the question. If you feel you have the answer to your own question in how to enable this on the Raspberry Pi, feel free to write your own answer and accept it, for the benefit of others. In any case, don't leave the question open, as it's no longer in need of an answer. – Grismar Nov 05 '20 at 23:49
  • I've already posted an answer to my question, though it won't allow me to accept it saying I have to wait 8 more hours to accept it – Sheepposu Nov 06 '20 at 14:49

1 Answers1

1

I figured out that I was getting the error because my network was not yet up. So I changed my shell script by adding this line of code before I executed the python scripts:

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

I got the suggestion from here: https://www.raspberrypi.org/forums/viewtopic.php?t=148716 and the code from here: Waiting for network link to be up before continuing in bash

Sheepposu
  • 79
  • 9