1

I've setup a telegram bot that notifies my users when the Ubuntu 20.04 machine is up out of hibernation. I'm doing this using the requests python library:

api_url = 'https://api.telegram.org/bot{token}/{method}'.format

def telegram_command(token, name, data): url = api_url(token=token, method=name) return requests.post(url=url, json=data)

Now, whenever I call this function under normal conditions, there is no problem. I put it into /etc/hibernate/common.conf as follows

### misclaunch
OnSuspend 20 python /usr/local/bin/tgnotify.py pre_hibernate

OnResume 20 python /usr/local/bin/tgnotify.py post_hibernate

When I activate 'sudo hibernate', the pre_hibernate message is sent without problems, but when it resumes from hibernation, the post_hibernate message does not get sent. Instead, exceptions are raised

Traceback (most recent call last):
  File "/usr/local/bin/slurm_cleanup.py", line 73, in resume_slurm_jobs
    for id in jobids:
TypeError: 'NoneType' object is not iterable
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 159, in _new_conn
    conn = connection.create_connection(
  File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 61, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "/usr/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

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 665, in urlopen httplib_response = self._make_request( File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 376, in _make_request self._validate_conn(conn) File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 996, in _validate_conn conn.connect() File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 314, in connect conn = self._new_conn() File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 171, in _new_conn raise NewConnectionError( urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x7f771089ca90>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/usr/lib/python3/dist-packages/requests/adapters.py", line 439, in send resp = conn.urlopen( File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 719, in urlopen retries = retries.increment( File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 436, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot<token removed>/sendMessage (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f771089ca90>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/usr/local/bin/tgnotify.py", line 90, in <module> result = telegram_sendMessage(api_key, message, chat_id) File "/usr/local/bin/tgnotify.py", line 69, in telegram_sendMessage return telegram_command(api_key,'sendMessage', { File "/usr/local/bin/tgnotify.py", line 66, in telegram_command return requests.post(url=url, json=data) File "/usr/lib/python3/dist-packages/requests/api.py", line 116, in post return request('post', url, data=data, json=json, kwargs) File "/usr/lib/python3/dist-packages/requests/api.py", line 60, in request return session.request(method=method, url=url, kwargs) File "/usr/lib/python3/dist-packages/requests/sessions.py", line 533, in request resp = self.send(prep, send_kwargs) File "/usr/lib/python3/dist-packages/requests/sessions.py", line 646, in send r = adapter.send(request, kwargs)

However, sending the post-hibernate message from Shell after resuming works without errors. The exceptions suggest that maybe the network is not up when hibernate runs trhe script. However, according the the SCRIPTLETS-API hibernate docs say that ordering the script at 20 should run the script before networking is down (during hibernation) and after the network is up during resume. So I'm confused as to why this is happening.

Update on 20230310:

As per suggestions in the comments, I updated the config in /etc/hibernate/common.conf with:

#OnResume 20 python /usr/local/bin/tgnotify.py post_hibernate
OnResume 20 bash -c '(sleep 10; python /usr/local/bin/tgnotify.py post_hibernate)& disown'

This seems to have resolved it, but I'm puzzled as to why. All this modification does is delay the activation of the bot by 10 seconds, then run the activation as a background process. Why does this solve the previous problem that there was no network? Also, why 10 seconds specifically? What if the network takes longer to start? According to the scriptlet docs, the commands in common.conf should execute in reverse order on resume, and that the network is starting/stopping at level 60. This means that the network should have been running at level 20 during the resume.

  • 1
    Hi! Good to know that the suggested duplicate worked for you, but I think that the update you added in your question should better be a new question, linking to this one for context. ;) – BeastOfCaerbannog Mar 10 '23 at 15:54

0 Answers0