Need Push Notifications From Your Server? This Tool Is Better Than Discord

Summary
-
ntfy.sh is a free, cross-platform notification service.
-
This is ideal for receiving notifications from servers, home automation and your Raspberry Pi projects.
-
Use any script, code, or webhook that can send an HTTP POST request to the ntfy.sh API.
ntfy.sh allows you to send notifications to your devices from your own scripts, home labs or automation projects. It’s free and does all the work of pushing notifications to multiple platforms. All you have to do is know what to call it.
What is ntfy.sh?
ntfy.sh is a free notification app that works on Apple and Android mobile devices, as well as sending notifications to desktop web browsers. Its primary use case is for IT administrators and tech enthusiasts to get live updates on the status of their infrastructure, but you can use it to receive alerts from virtually any script or piece of code, or any service that supports webhooks.
Usually, to receive these types of push notifications, you need to develop a full app and go through the App Store approval process, or set up a web app with Web Push, so having something that has already done all of this for you saves you a lot of hassle.
Why do I use ntfy.sh?
I use ntfy.sh for a few useful and not-so-useful things: I get notifications when backups succeed and fail, and if one of my websites stops responding or a server needs a restart. My NAS reports when the disk or memory starts to run out, and I even have a temperature sensor connected to it, so I know if things are getting too hot in my server cabinet. On the less useful side, I get a smiley notification when a new user signs up for one of my apps.
Server monitoring is not the limit of this tool. You can integrate it into any home automation or script to make it send notifications. All you need to do is add the appropriate ntfy.sh command to your Bash or Python scripts.
You can integrate it with a sensor on your washing machine to receive a notification when it’s finished, or receive an alert if your garage door is left open for more than a few minutes. You can connect multiple sensors to a Raspberry Pi and use it to be informed about all kinds of things around the house.
How do you use ntfy.sh?
You can call ntfy.sh from any script, code, or webhook that can send an HTTP POST request to its API.
It works like this: you create a topic on an ntfy.sh server and subscribe to it on the ntfy.sh app (look for the App Store links on the ntfy.sh website or click “Application” to use the browser-based version). The topic name should be unique and secret, because anyone who knows it can read notifications (so in the examples below, be sure to replace it!).
Then from your scripts you simply send notifications about it and all subscribed devices receive the notification. You can use the public ntfy.sh server for free or replace the URL in the scripts with your own self-hosted instance. You can learn more about how to use it in the ntfy.sh documentation.
There is one important rule: do not post sensitive information there: only send alerts, do not include identifying information or credentials in your notifications, as they could be read and intercepted by others. Authentication is available, but usually the subject name acts as the password, which does not provide sufficient security for important information.
Here are some examples based on the snippets I use in my own scripts.
Using ntfy.sh to notify Bash script failure
Here are the commands I paste into my Bash scripts to send ntfy.sh notifications if the script fails at any point. It uses 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="Finished successfully on $(hostname)"
TITLE_FAIL="Job FAILED"
MESSAGE_FAIL="Script interrupted on $(hostname)"
notify() {
local title="$1"
local message="$2"
curl -fsS -H "Title: ${title}" -d "${message}" "${NTFY_URL}" || true
}
# Notify on any error and exit with the same status
trap 'rc=$?; notify "$TITLE_FAIL" "Script failed on $(hostname) (exit ${rc})"; exit "${rc}"' ERR
# Notify if user hits Ctrl+C and exit with 130 (SIGINT)
trap 'notify "$TITLE_FAIL" "$MESSAGE_FAIL"; exit 130' INT
# -----------------------------
# Your script goes here
# Any non-zero exit triggers the ERR trap above
# -----------------------------
# Notify on success
notify "$TITLE_OK" "$MESSAGE_OK"
The trap command detects errors and calls the notify() function, which sends a request to NTFY_URL, passing it the TITLE_OK and TITLE_FAIL values for display on your device.
Using ntfy.sh with PowerShell and Windows Task Scheduler
Windows Task Scheduler not only runs on a timer, it can also perform actions when specific events are triggered. Events such as your PC running out of disk space or needing to restart after updates can be used to trigger PowerShell scripts.
The PowerShell script below should be saved in its own file (e.g. my_ntfy_script.ps1). It accepts a title, message, and URL for an ntfy.sh server, then sends the provided notification.
param(
[string]$Title = "Windows Event",
[string]$Message = "Triggered",
[string]$Url = "https://ntfy.sh/my_super_secret_topic"
)
try {
# Make HTTP Request to ntfy.sh API
Invoke-RestMethod -Method Post -Uri $Url -Headers @{ Title = $Title } -Body $Message -ErrorAction Stop
} catch {
# Write to the Application log on failure
Write-EventLog -LogName Application -Source "Windows PowerShell" `
-EventId 3001 -EntryType Warning `
-Message "ntfy.sh notify failed: $($_.Exception.Message)"
}
You can then create an event-triggered task in Windows Task Scheduler. This example sends a notification when a user attempts to log in unsuccessfully, which creates an event with ID 4625 in the security log.
To create a scheduled task for this purpose, open Task Scheduler by searching for it in the Windows Start menu, then select Action > Create Basic Task from the toolbar. In the Create Task dialog box, fill in the name and optional description of the task (for example, Notification of failed connection to ntfy.sh), click “Next”, then set the trigger to “When a specific event is recorded”, then click “Next” again.
To trigger a notification when your PC restarts, set the log value to Securityand set the EventID to 4625and again press “Next”.
Then select the action to start a program and press the Next button again. Copy the command below into the Program/script field, replacing the path to your script and the topic name.
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\Scripts\my_ntfy_script.ps1" -Title "Login failed" -Message "Someone tried to access your PC" -Url "https://ntfy.sh/my_super_secret_topic"
Click “Next” again. A warning may be displayed regarding the arguments provided in the Program text box: this is okay, they will be converted to arguments when saving. Click Finish on the next screen when you are finished.
When you’re done, the new task should appear in the Task Scheduling Library list as shown above. To test, you can right-click the task and select “Run”.
Note that these notifications do not contain any revealing information, such as IP addresses or usernames. I know what the notifications mean, but if anyone else were to guess the name of the ntfy.sh topic, they wouldn’t be able to do anything with the information.
ntfy.sh and macOS Automator
The Automator tool that comes with macOS is often overlooked or even overlooked, but it really can do a lot. It lets you automate common tasks with minimal coding, and you can even record yourself doing things and then repeat them. You can also embed Bash scripts, so the snippet provided above can be used here as well.
Subscribe from ntfy.sh apps
From the ntfy.sh app on your mobile device (or using your browser’s web app), tap Add Subscription, then enter your topic name (and custom server address if you’re using one).
The topic you are subscribed to will then appear in the list…
…And when your scripts are executed, you will be notified!
Quite simple! And certainly useful.
DIY Projects You’ll Want to Hear About Away from Home
If you don’t have anything to send notifications to yet, why not start a homelab (they’re very useful and allow you to self-host a bunch of apps, including ntfy.sh)?
You can also run your smart home from a Raspberry Pi using Home Assistant, which supports ntfy.sh for notifications. If you want to tinker, you can try some of these Raspberry Pi projects from the weekend (and a few other projects here and here!) and see if there’s anything you can add to ntfy.sh that hasn’t been done yet.



