How do you get phone notifications when long-running commands finish?
Asked by
MellowPine42
I regularly run large backups and other commands that can take a long time. I'd like a way to know when one finishes, preferably through a notification on my phone, although desktop alerts, sound, or email would also work. What notification methods or shell setups have you found practical without being too complicated?
Answers
If the job is running in the background, use the shellโs `wait` builtin rather than polling the process manually: `long_backup & pid=$!; wait "$pid" && notify-send 'Backup complete' || notify-send 'Backup failed'`. For an already-running process, a small loop that checks its PID can work, followed by an alert when it exits.
Reply to this answer
For local desktop alerts, use notify-send from libnotify. You can add it after a command, for example: `long_backup && notify-send 'Backup finished' || notify-send 'Backup failed'`. If you want this to happen automatically, a shell hook can measure how long interactive commands take and notify you only when they exceed a threshold. Itโs also possible to suppress notifications when the terminal window still has focus.
Reply to this answer
Email is still one of the simplest and most reliable approaches, especially for servers. You can pipe command output into mail and receive the result on your phone, where you can mark messages from yourself as high priority. It also leaves an archive of what happened, which can be useful for backups and other maintenance jobs.
Reply to this answer
For a one-off command, chaining a sound or another local action is often enough: `long_backup && play success.wav || play failure.wav`. You can substitute any command that makes sense, such as a desktop popup, a script that calls a push service, or a request to a device on your network. The main thing is to handle success and failure separately so you donโt mistake a failed backup for a completed one.
Reply to this answer
For phone notifications, ntfy and Gotify are both popular options. You can send a message with a simple HTTP request from a shell script. Gotify is especially nice if you want to host it yourself on a home server, VM, container, or LAN, while ntfy is quick to get started with. Telegram bots, Pushover, and similar push services are other straightforward choices.
BrightKite19: Iโve had good results with Gotify too. The Android app and curl-based API make it easy to trigger from backup scripts.
Reply to this answer
Add your answer
Answers are reviewed before publication.
SilverNook6: A short delay combined with checking whether the terminal has focus helps avoid notifications for commands that intentionally open another GUI window.