1

I wrote a Python script which sends any message via Telegram:

#!/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 is set in /etc/environment, MYCHATID is hardcoded.

Created two scripts to run it

# 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..."

Added one of them to cron

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

And another one to 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

All scripts are executable and work well when I run them from terminal. But neither of them work on actual reboot.

What I did wrong?

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

I have grabbed error output and got:

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
  • cron doesn't care about /etc/environment Commented Feb 9, 2023 at 23:48

1 Answer 1

0

It looks like the scripts are not executing properly on reboot. On Ubuntu 18.04, system services and scripts in the /etc/rcX.d/ directories are executed with the /sbin/runlevel program. The init scripts in /etc/init.d/ can be executed directly, but they must also be linked to the appropriate /etc/rcX.d/ directory in order to be executed at boot time or when changing runlevels.

Try creating a symlink for your init script in the appropriate rcX.d directory using the following command:

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

This should ensure that your scripts are executed on boot and shutdown.

PS: Try to update Ubuntu :-)

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.