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
cron
doesn't care about/etc/environment