起動または終了中に実行されるPythonスクリプトでは、名前解決が一時的に失敗します。

起動または終了中に実行されるPythonスクリプトでは、名前解決が一時的に失敗します。

Telegram経由でメッセージを送信するPythonスクリプトを作成しました。

#!/opt/anaconda3/bin/python

import asyncio
import telegram
import os
import sys


async def main():
    bot = telegram.Bot(os.environ["TELEGRAM_TOKEN"])
    async with bot:

        #print(await bot.get_me())
        #print((await bot.get_updates())[0])
        await bot.send_message(text=' '.join(sys.argv[1:]), chat_id=MYCHATID)


if __name__ == '__main__':
    asyncio.run(main())

TELEGRAM_TOKEN設定がハードコードされてい/etc/environmentますMYCHATID

これを実行するために2つのスクリプトを作成しました。

# cat /etc/init.d/sendbot_boot.sh
#!/bin/bash

/opt/anaconda3/bin/python /usr/local/bin/sendbot "Booting..."

# cat /etc/init.d/sendbot_shutdown.sh
#!/bin/bash

/opt/anaconda3/bin/python /usr/local/bin/sendbot "Shutting down..."

次のいずれかを追加してください。cron

# cat /var/spool/cron/crontabs/root
@reboot /etc/init.d/sendbot_boot.sh

別の人が到着していますrc6

# ls -l /etc/rc6.d/K99sendbot_shutdown
lrwxrwxrwx 1 root root 29 фев 10 02:08 /etc/rc6.d/K99sendbot_shutdown -> ../init.d/sendbot_shutdown.sh

すべてのスクリプトは実行可能で、端末で実行すると正しく実行されます。しかし、それらのどれも実際の再起動では機能しません。

私は何が間違っていましたか?

# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.6 LTS
Release:        18.04
Codename:       bionic

エラー出力を確認し、以下を取得しました。

Traceback (most recent call last):
  File "/opt/anaconda3/lib/python3.8/site-packages/httpcore/_exceptions.py", line 10, in map_exceptions
    yield
  File "/opt/anaconda3/lib/python3.8/site-packages/httpcore/backends/asyncio.py", line 111, in connect_tcp
    stream: anyio.abc.ByteStream = await anyio.connect_tcp(
  File "/opt/anaconda3/lib/python3.8/site-packages/anyio/_core/_sockets.py", line 189, in connect_tcp
    gai_res = await getaddrinfo(
  File "/opt/anaconda3/lib/python3.8/site-packages/anyio/_core/_sockets.py", line 496, in getaddrinfo
    gai_res = await get_asynclib().getaddrinfo(
  File "/opt/anaconda3/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 1754, in getaddrinfo
    result = await get_running_loop().getaddrinfo(
  File "/opt/anaconda3/lib/python3.8/asyncio/base_events.py", line 825, in getaddrinfo
    return await self.run_in_executor(
  File "/opt/anaconda3/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/opt/anaconda3/lib/python3.8/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

ベストアンサー1

再起動時にスクリプトが正しく実行されないようです。 Ubuntu 18.04では、/etc/rcX.d/ディレクトリのシステムサービスとスクリプトはプログラムによって実行されます/sbin/runlevel。の初期化スクリプトは直接実行できますが、起動時またはランレベルの変更時に実行するには/etc/init.d/適切なディレクトリに接続する必要があります。/etc/rcX.d/

次のコマンドを使用して、適切なrcX.dディレクトリにinitスクリプトへのシンボリックリンクを作成します。

sudo ln -s /etc/init.d/sendbot_boot.sh /etc/rc2.d/S99sendbot_boot
sudo ln -s /etc/init.d/sendbot_shutdown.sh /etc/rc0.d/K99sendbot_shutdown

これにより、起動時と終了時にスクリプトが実行されます。

PS:Ubuntuを更新してみてください :-)

おすすめ記事