Last active
August 28, 2023 15:05
-
-
Save MarkWalters-dev/3c02823a6bd111c90bad8344052143e6 to your computer and use it in GitHub Desktop.
Simple script to let me know that a long running command is done. Uses ntfy, dunst/notify-send, and is ssh aware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
#🌩️ gist 3c02823a6bd111c90bad8344052143e6 alert | |
# Simple script to let me know that a long running command is done | |
# It uses both the ntfy service and either notify-send or dunst | |
# If it is running over a ssh session it will also try to use your local computers notify service | |
# Typical usage in a script: | |
# . $HOME/.bin/common/alert | |
# alert my message | |
# You can also add the following to .bashrc | |
# . $HOME/.bin/common/alert | |
# Then use it from the command prompt | |
# myLongCmd ; alert it is finally done | |
# Using curl instead of ntfy-sh so I don't have additional dependencies | |
# Using source instead of a normal bash script so I can see which program is calling this one | |
# Before using you must replace MYTOPIC with your ntfy topic and DEFAULTUSER with your username | |
# TODO: | |
# support other services https://wiki.archlinux.org/title/Desktop_notifications#Notification_servers | |
# Add hostname and caller to local_alert | |
# Add title and additional tags support | |
# Self host ntfy server | |
# remove hard coded ntfy topic and hide it in a secrets service | |
# Make it zsh compatible | |
# nix flake | |
command_exists() { | |
command -v $1 > /dev/null 2>&1 | |
return $? | |
} | |
crash() { | |
local err=$? | |
printf "%s\nCrashed at: %s:%s\n" "$*" "${FUNCNAME[1]}" "${BASH_LINENO[0]}" | |
case ${BASH_SOURCE[0]} in | |
local_alert | alert | crash) | |
# Cant exit if directly calling the function from bash | |
return $err | |
;; | |
*) | |
exit $err | |
;; | |
esac | |
} | |
local_alert() { | |
if command_exists notify-send; then | |
notify-send "$*" || crash "notify-send error" | |
elif command_exists dunstify; then | |
dunstify "$*" || crash "dunst error" | |
elif command_exists termux-toast; then | |
termux-toast "$*" || crash "termux-toast error" | |
fi | |
} | |
alert() { | |
local tags caller ssh_ip | |
[ -z "$1" ] && { echo "Usage: $(basename ${BASH_SOURCE[0]}) YOUR MESSAGE"; return; } | |
command_exists curl || { echo "curl not installed"; return; } | |
# Find out who is calling me | |
if [ -n "${BASH_SOURCE[2]}" ]; then | |
caller="${BASH_SOURCE[2]},${BASH_SOURCE[1]}" | |
elif [ -n "${BASH_SOURCE[1]}" ]; then | |
caller="${BASH_SOURCE[1]}" | |
else | |
caller="${BASH_SOURCE[0]}" | |
fi | |
caller=${caller##*/} # Same as using basename | |
tags="$HOSTNAME" # Always show the hostname | |
# Add program name unless alert was manually ran from bash | |
[[ "$caller" != "alert" ]] && tags+=",$caller" | |
curl -s -H "Tags: $tags" -d "$*" ntfy.sh/MYTOPIC > /dev/null || crash "Curl error" | |
# curl -s -H "Title: meow" -H "Tags: $tags" -d "$*" ntfy.sh/MYTOPIC > /dev/null || crash "Curl error" | |
# Also send a notification to your local computer | |
ssh_ip=${SSH_CLIENT%% *} | |
[ -n "$ssh_ip" ] && { | |
ssh DEFAULTUSER@$ssh_ip "$(declare -f local_alert command_exists crash);local_alert $*" || crash "ssh error" | |
} || { | |
local_alert $* | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment