Created
March 16, 2026 02:38
-
-
Save chris/9baa42dc3e57162196332f17c87db455 to your computer and use it in GitHub Desktop.
Run a command via nohup, and then send a push notification on success/failure (shell function)
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
| # Shell function to run a command via nohup, with output logged to a file based on the name of the notification label. | |
| # Then when the command finishes (success or failure), send a push notification to alert it's done. | |
| # Log file name will have spaces from the message name with underscores. | |
| # Usage examples: | |
| # runnotify "Build" ./build.sh | |
| # runnotify "Tests" go test ./... | |
| # runnotify "Deploy" sls deploy -s production | |
| runnotify() { | |
| local prefix="${1:-Command}" | |
| shift | |
| local logfile="${prefix// /_}_$(date +%Y%m%d_%H%M%S).log" | |
| { | |
| nohup "$@" >> "$logfile" 2>&1 | |
| if [ "$?" -eq 0 ]; then | |
| curl -s -d "${prefix} succeeded" ntfy.sh/crbalertz | |
| else | |
| curl -s -d "${prefix} failed" ntfy.sh/crbalertz | |
| fi | |
| } & | |
| disown | |
| echo "Running in background (PID: $!). Logging to: $logfile" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment