ntfy - Supercharging Your Bash Scripts
I wanted to take the time to write about a tool that has been invaluable to me across many projects. If you haven’t heard of ntfy and you run any kind of recurring or long running jobs that are hard to monitor, prepare to be mind-blown.
ntfy is a free pub-sub service with outstanding DX that makes sending push notifications to your phone an absolute dream.
Here’s how to get started:
-
Go to ntfy.sh or download the mobile app and sign up for a free account
-
Subscribe to a new topic (I like to use a uuid as the topic name)
-
Make a curl request from the command line to your topic
curl -d "Hello! 😀" ntfy.sh/<topic_uuid>
-
Boom - you just sent your phone a push notification from the command line!
Here’s how I use ntfy in my work:
As part of my research, I launch and manage electromagnetic simulations of hundreds of thousands of on-chip structures. These simulations are run on a remote server managed by my school. Sometimes there is unscheduled downtime, unexpected server restarts, and even job cancellation (since the server is shared among many researchers). On top of this, electromagnetic simulations can fail, take very long to converge, or otherwise have issues that need to be dealt with. To handle all of these issues, I setup very simple progress monitoring using ntfy!
In my simulation job launcher script I add the following function:
def send_notification(title, message, tags):
subprocess.run([
"curl", "-H", f"Title: {title}", "-H", f"Tags: {tags}", "-d", message, ntfy_endpoint
])
ntfy_endpoint is a global variable that stores the ntfy url with the topic uuid to publish to.
Then, as my simluations progress, I trigger a notification after every x simulations:
minutes = datetime.now() - datetime.strptime(start_timestamp, "%Y-%m-%d %H:%M:%S")
minutes = minutes.total_seconds() / 60
send_notification("Simulation Update", f"Starting Simulation for {gds_file}. Simulation has been running for {minutes:.2f} minutes.", "triangular_flag_on_post")
Essentially one line of code in any python or bash script and you instantly have the power to send push notifications to your phone. So powerful.
NOTE:
It is worth noting that ntfy topics are public and your notifications could potentially be read by anyone. Don’t send any sensitive info in an ntfy topic.
If you need privacy, you can either pay for ntfy pro or self host ntfy and access it securely (through a vpn, cloudflare tunnel, etc.).
Hope this helps you.