Abstract

  • ntfy.sh is a free, cross-platform notification service.
  • It is nice for receiving notifications from servers, dwelling automation, and your Raspberry Pi tasks.
  • Use any script, code, or webhook that may make a POST HTTP request to the ntfy.sh API.

ntfy.sh helps you to ship notifications to your units from your individual scripts, dwelling lab, or automation tasks. It is free and does all of the work of delivering notifications to a number of platforms. All that you must do is know the best way to name it.

What’s ntfy.sh?

ntfy.sh is a free notification app that works on Apple and Android cellular units, in addition to delivering notifications to desktop internet browsers. It is main use case is for IT directors and tech fans to get dwell standing updates on their infrastructure, however you need to use it to obtain alerts from just about any script or little bit of code, or any service that helps webhooks.

Often, to get these sorts of push notifications, you’d should develop an entire app and undergo the app retailer approval course of, or arrange an internet app with web push, so having one thing that is already completed all that for you saves plenty of trouble.

What Am I Utilizing ntfy.sh For?

I’ve acquired a number of helpful and less-useful issues I am utilizing ntfy.sh for: I obtain notifications when backups succeed and fail, and if one among my web sites stops responding or a server wants a reboot. My NAS experiences when disk or reminiscence begins working low, and I actually have a temperature sensor hooked up to it, so I do know if issues are getting too toasty in my server cupboard. On the less-useful facet, I get a smiley face notification when a brand new person indicators up for one among my apps.

Server monitoring is not the restrict of this device. You possibly can combine it into any dwelling automation or script to make it ship notifications. All that you must do is add the related ntfy.sh command to your Bash or Python scripts.

You may combine it with a sensor in your washer to get a notification when it is completed, or get an alert in case your storage door is left open for quite a lot of minutes. You possibly can join a bunch of sensors to a Raspberry Pi and use it to get notified about all types of issues round the home.

How Do You Use ntfy.sh?

You possibly can name ntfy.sh from any script, code, or webhook that may make a POST HTTP request to its API.

It really works like this: You create a subject on a ntfy.sh server, and subscribe to it on the ntfy.sh app (discover the app retailer hyperlinks on the ntfy.sh website, or click on ‘App’ to make use of the browser-based model). The title of the subject must be distinctive and secret, as anybody who is aware of it could actually learn the notifications (so, within the beneath examples, ensure you change it!).

Then, out of your scripts, you simply ship notifications to that subject, and any subscribed units obtain the notification. You should use the general public ntfy.sh server without spending a dime, or change the URL within the scripts with your individual self-hosted occasion. You will discover out extra about the best way to use it within the ntfy.sh documentation.

There’s one necessary rule: do not publish delicate data to it—solely ship alerts, do not embody credentials or figuring out data in your notifications as they could possibly be learn and intercepted by others. Authentication is obtainable, however typically the title of the subject acts because the password, which isn’t adequate safety for necessary information.

Listed below are a number of examples primarily based on the snippets I take advantage of in my very own scripts.

Utilizing ntfy.sh To Notify on Bash Script Failure

Listed below are the instructions I paste into my Bash scripts to ship ntfy.sh notifications if the script fails at any level. It makes use of curl to make the HTTP request.

#!/bin/bash
set -Eeuo pipefail
NTFY_URL="https://ntfy.sh/my_super_secret_topic"
TITLE_OK="Job COMPLETED"
MESSAGE_OK="Completed efficiently on $(hostname)"
TITLE_FAIL="Job FAILED"
MESSAGE_FAIL="Script interrupted on $(hostname)"
notify() {
native title="$1"
native message="$2"
curl -fsS -H "Title: ${title}" -d "${message}" "${NTFY_URL}" || true
}
# Notify on any error and exit with the identical standing
entice 'rc=$?; notify "$TITLE_FAIL" "Script failed on $(hostname) (exit ${rc})"; exit "${rc}"' ERR
# Notify if person hits Ctrl+C and exit with 130 (SIGINT)
entice 'notify "$TITLE_FAIL" "$MESSAGE_FAIL"; exit 130' INT
# -----------------------------
# Your script goes right here
# Any non-zero exit triggers the ERR entice above
# -----------------------------
# Notify on success
notify "$TITLE_OK" "$MESSAGE_OK"

The entice command catches errors and calls the notify() operate, which sends a request to the NTFY_URL, passing the TITLE_OK and TITLE_FAIL values to it for show in your gadget.

Utilizing ntfy.sh with PowerShell and the Home windows Process Scheduler

The Windows Task Scheduler would not simply work on a timer, it could actually additionally carry out actions when particular occasions are raised. Occasions like your PC working out of disk house or needing to reboot after updates can be utilized to set off PowerShell scripts.

The beneath PowerShell script must be saved in its personal file (e.g. my_ntfy_script.ps1). It accepts a title, message, and URL for a ntfy.sh server, then sends the equipped notification.

param(
[string]$Title = "Home windows Occasion",
[string]$Message = "Triggered",
[string]$Url = "https://ntfy.sh/my_super_secret_topic"
)
strive {
# Make HTTP Request to ntfy.sh API
Invoke-RestMethod -Methodology Put up -Uri $Url -Headers @{ Title = $Title } -Physique $Message -ErrorAction Cease
} catch {
# Write to the Software go surfing failure
Write-EventLog -LogName Software -Supply "Home windows PowerShell" `
-EventId 3001 -EntryType Warning `
-Message "ntfy.sh notify failed: $($_.Exception.Message)"
}

You possibly can then create an event-triggered process within the Home windows Process Scheduler. This instance sends a notification when a person makes an attempt to log in and is unsuccessful, which creates an occasion with ID 4625 within the Safety log.

To create a scheduled process for this, open the Process Scheduler by trying to find it within the Home windows Begin menu, then choose Motion > Create Primary Process from the toolbar. Within the Create Process dialog, fill within the Title and non-compulsory Description for the duty (for instance, ntfy.sh login failed notification), click on “Subsequent,” then set the Set off to “When a particular occasion is logged”, then click on “Subsequent” once more.

To set off a notification when your PC reboots, set the Log worth to Safety, and set the EventID to 4625, and once more, press “Subsequent.”

Scheduling a task in the Task Scheduler triggered by an event.

Subsequent, choose the Motion to Begin a program, and hit the Subsequent button as soon as extra. Copy the beneath command into the Program/script subject, changing the trail to your script and subject title.

powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:Scriptsmy_ntfy_script.ps1" -Title "Login failed" -Message "Somebody tried to entry your PC" -Url "https://ntfy.sh/my_super_secret_topic"
Configuring a PowerShell script in the Task Scheduler.

Click on “Subsequent” once more. Chances are you’ll be offered with a warning about arguments being offered within the Program textual content field—that is OK, they will be transformed into arguments whenever you save. Click on End on the subsequent display screen whenever you’re completed.

Windows Task Scheduler entries.

Whenever you’re completed, the brand new process ought to seem within the Process Schedule Library record as proven above. To check, you’ll be able to right-click on the duty and choose “Run.”

Word that these notifications do not include any revealing data, equivalent to IP addresses or usernames. I do know what the notifications imply, but when another person had been to in some way guess the ntfy.sh subject title, they cannot do something with the information.

ntfy.sh and macOS Automator

The Automator device that ships with macOS is usually missed, is presumably uncared for, however it could actually actually do loads. It helps you to automate widespread duties with minimal coding, and you’ll even document your self doing issues after which have it repeat them. You possibly can incorporate Bash scripts, too, so the snippet offered above can be utilized right here as effectively.

Subscribing From the ntfy.sh Apps

From the ntfy.sh app in your cellular gadget (or utilizing the online app out of your browser), faucet Add Subscription, then enter your subject title (and customized server handle should you’re utilizing one).

Adding a subscription to ntfy.sh.

The subscribed subject will then seem within the record…

Viewing subscribed topics in ntfy.sh.

… And when your scripts run, you will get notified!

Notification received on an iPhone from ntfy.sh.

Fairly easy! And positively helpful.

DIY Initiatives You’ll Need To Hear About Away From Residence

If you have not acquired something to ship notifications about but, why not start a homelab (they’re super useful and allow you to self-host a bunch of apps, together with ntfy.sh)?

Or, run your smart home from a Raspberry Pi utilizing Residence Assistant, which helps ntfy.sh for notifications. If you wish to tinker, you’ll be able to strive a few of these weekend Raspberry Pi projects (and a few more projects here, and here!) and see if there’s something you’ll be able to add ntfy.sh to that hasn’t been completed but.


Source link