From b7e54f50851463ff7535d694d67853b19da546a9 Mon Sep 17 00:00:00 2001 From: Adar Nimrod <nimrod@shore.co.il> Date: Thu, 2 Sep 2021 17:36:33 +0300 Subject: [PATCH] Don't hang on sending notification to Nextcloud. I tried calling curl with & at the end, but that caused output from the notify script to spam the prompt (process XXXX has finished). Instead I wrote a short Python script to nicely fork and silently send the message in the background, quickly returning to the calling process (the Bash prompt). For reference, the problem I'm trying to solve is that on remote systems, sending the notifications and has curl waiting for the response. This causes the prompt to hang for a few seconds which I find irritating. --- Documents/bin/nc-notify | 25 +++++++++++++++++++++++++ Documents/bin/notify | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100755 Documents/bin/nc-notify diff --git a/Documents/bin/nc-notify b/Documents/bin/nc-notify new file mode 100755 index 0000000..62a270d --- /dev/null +++ b/Documents/bin/nc-notify @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +# pylint: disable=invalid-name +"""Send a notification to my notification service. + +Send it in a separate process so that the calling process is not hanged. +""" + +import json +import os +import sys +import urllib.request + + +if __name__ == "__main__": + if os.fork(): + sys.exit() + message = " ".join(sys.argv[1:]) + data = json.dumps({"message": message}).encode() + request = urllib.request.Request( + "https://notify.shore.co.il/send", + data=data, + method="POST", + headers={"Content-Type": "application/json"}, + ) + urllib.request.urlopen(request) # nosec diff --git a/Documents/bin/notify b/Documents/bin/notify index c5551b2..c2ce2d3 100755 --- a/Documents/bin/notify +++ b/Documents/bin/notify @@ -16,7 +16,7 @@ notify_send () { nextcloud () { message="$(cat)" - curl --silent -d "$message" https://notify.shore.co.il/send > /dev/null + nc-notify "$message" } # Try hostname if present. -- GitLab