Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Show HN: Alert yourself after a long-running task in terminal (gist.github.com)
86 points by petethepig on Feb 25, 2021 | hide | past | favorite | 50 comments


Personally I have a flask app deployed on heroku that posts a message to a Discord webhook just by visiting an url. So in my case when I use ssh to run long tasks I have at the end "curl https:// <server> .herokuapp.com/?Finished" and as soon as that commands is executed I receive a message either at my phone or computer (through a channel in a personal discord server).

Probably is a bit of a 'hacky' way, and using different proprietary services (heroku and discord) but it works, does it fine, and it's free! (And it was also funny and quick to develop by myself).

The source code is on Github too! https://github.com/TrianguloY/ping


This seems a little over complicated since curl itself could just post to discord.


I guess so, the app is simply a middleware and if you check the source code it is basically a fetch to the discord api url with the specific json it needs.

But personally I made it for two reasons: no need to know the discord webhook api (a long alphanumeric url) nor the json required, so I can use it wherever I want without having to remember almost anything; and as a url to be able to use it from any internet-connected thing, not only with curl, even the browser if you want.


I have a similar thing with telegram bot: https://pastebin.com/Ei3hjcKa Written in Go so it's static and portable.


This is a cool idea, I've always wanted to expand this kind of thing to other similar usecases like getting notified after long CI jobs.

I think the repo is private though.


Ouch, thanks for noticing, should be public now.

I also made a similar service in the past for Heroku webhooks, to be notified with heroku apps status. Github already have one built-in. https://github.com/TrianguloY/webhook-discord

Webhooks are a really interesting feature!


I hear that `terminal-notifier` is pretty much dead and a better replacement with new MacOS APIs is [0]. In any case, I opted against it because iTerm actually supports this natively, in addition to a few other neat features, and it will work over SSH. I wrote about my solution here [1].

[0] https://github.com/sheagcraig/yo [1] https://cgamesplay.com/post/2020/12/09/iterm-notifications/


There is a native feature for this however gnome-terminal won't ship it largely due to disagreement how to integrate it with shells. However Fedora includes patches for it and I use it every day and it is amazing. The best thing about the native integration is that it notifies for every command completion where the terminal isn't focused. This means that when I am watching the terminal I don't get distracting notifications at the top of my screen and closing vim or less even if it has been open for a while doesn't trigger it either. The time based approach is just not as good as you may miss short commands (especially unexpectedly short, so you switch away because you think it will take a while then are confused when you don't see the notification you expected) and get bothered unnecessarily when you are looking at the terminal.

Bug: https://bugzilla.gnome.org/show_bug.cgi?id=795774

VTE Patch: https://src.fedoraproject.org/rpms/vte291/raw/623150318ddb9a...

gnome-terminal Patch: https://src.fedoraproject.org/rpms/gnome-terminal/raw/bb5f88...


Here is it for fish, using the postexec event handler:

    function postexec_notify --arg cmd --on-event fish_postexec
      set -l s $pipestatus
      if test "$CMD_DURATION" -ge 25000 && ! string match -rq '(^man|\s--help)(\s|$)' $cmd
        if string match "$status" -qv 0 $s
          terminal-notifier -title $cmd -message 'Exited with '(string join '|' $s)' after '(math $CMD_DURATION / 1000)s
        else
          terminal-notifier -title $cmd -message 'Finished in '(math $CMD_DURATION / 1000)s
        end
      end
    end
As a bonus, this includes the command line in the notification.

I found that this can be somewhat annoying when reading man pages or interactive content though. To remedy this, I tried detecting whether the terminal is focused at the time of command completion, but it doesn't seem possible to get the terminal's pid from fish. The pid of the topmost application can be retrieved with:

    osascript -e 'tell application "System Events" to return unix id of (first process whose its frontmost is true)'


Edit: line 4 should be `if string match -qv 0 $s`.


I use ohmyzsh with the bgnotify plugin that does the same.

I combine it with the timer plugin to print how long the command takes to complete.

I'd argue that most developers should become familiar with ohmyzsh, it has a bunch of useful plugins. My favorites being fzf, git, timer and bgnotify, and I love that tiny green arrow that tells you if the last command was successful.


I use `noti` for this:

`some_task && noti`

`noti some_task`

both work.

https://github.com/variadico/noti


You can also use noti to watch a process id, in case you start a process but forget to append noti


wow, I didn't know this, thanks for the info. Sometimes it happens that I run something and after 30 minutes think "Hmm, would have been great to add noti"


This is great, thanks for sharing.


I've recently coded something like this, however with significant differences from what OP is presenting. Here are the most important differences:

- Using a zsh plugin, it works for every command that I run, but only if I need it, so it doesn't create extra noise. Meaning, it is automatically activated for terminals that go out of focus. I don't need a special suffix to activate it. I recently contributed a tmux pull request just for that [1].

- It's a fixed size, flashing window in a side monitor, so if I got off the chair after execution started, I still notice the termination when I return to my workstation.

- If I change my focus back to the unfocused terminal where the termination happened, the notification disappear by itself (no extra action needing to click the 'x'!).

- It contains information regarding the tmux pane in which the termination has happened. This is needed, because I have multiple tmux sessions each having multiple windows with multiple panes in it, and all this on top of multiple XFCE workspaces.

- It's a server-client architecture, so clients running on separate host shells can run on a different computer and am able to send over termination notifications to the server that hosts the GUI (my workstation desktop).

[1] https://github.com/tmux/tmux/pull/2558


I wrote Notica (https://notica.us) to do something similar. It uses web browser notifications so it works on the servers you are SSH'd into and also to your phone.


OP is on Mac, which has the open command, which will open its arguments the same way they'd be opened by the Finder.

My solution is usually as follows:

   $ long-running-command; open song.mp3


You could also use `say`


`say $?` would be pretty nice.


Sounds like the open command is similar to xdg-open.


On Gnome (via libnotify):

    sleep 2 && notify-send "Done"


This probably works in any free desktop environment :-)

notify-send can be installed with the ruby-notify package.


The dialog box from notify-send can be customized using facilities of the DE. I happen to use it with XFCE, and customization is then under "Notifications".



`A + option + command` to be alerted on the next mark, which is basically when the new prompt appears.


A lot of options here, so I'd like to add another :)

On cygwin/win10, I have an alias for 'wakeme', which under the hood uses Snoretoast, then I just append '; wakeme' to the command I run.

alias wakeme='date && /cygdrive/c/tools/snoretoast/snoretoast.exe -t "hello" -m "wake up" -p "C:\Dropbox\tools\mindblown.png" -s "ms-winsoundevent:Notification.Reminder"'


Reminds me of my homegrown version of a notifier. Essentially we had a lot of long running tasks that would emit logs. My terminal notifier:

1. Scanned for patterns in these logs periodically.

2. Send out a mail to my Gmail account with specific subjects.

3. I had set up IFTTT[1] on my mobile (android) to scan my inbox and look for certain subject lines^, and trigger playing music.

Worked like a charm for the ~6 months I was on the project.

^ Maybe bodies too, can't remember now, this was in 2015-2016.

[1] https://ifttt.com/

EDIT: I should also note this workflow encouraged quite a bit of fun social interaction. My team knew of this setup, and when my phone would randomly play music, everyone would break into laughter as I hurried back to my desk to troubleshoot!



There is an alias called alert in .bashrc in Ubuntu. You can use it as follows: sleep 3; alert


And on Mac OS:

    osascript -e "display notification \"Alert\""


I just typeahead something to give a beep or popup with text, like with a little script. :) Seems others here all have their own way to do similarly.

ps: maybe sem-related: for things that I want to run a long time ad want them to use less cpu while they do for whatever reason (like overheating), I wrote a bash script I called "verynice" that makes everything in the same process group as the program it executes, all go to sleep for N seconds, then wakes/runs them again for M seconds afterward, in a loop. And when it finishes, (ie, the sleep/wake target not running any more), it exits. Then the typeahead noted above could finally execute and beep or pop up a message.


Strange how many commenters misunderstood this. This alerts when any command runs for a long time, without having to modify the command itself.


For Ubuntu/Gnome users, you can substitute `notify-send` for `terminal-notifier`, as linked.[1]

For WSL users, you can substitute a powershell call to BurntToast[2]

[1]https://gitlab.com/victor-engmark/tilde/-/commit/1fc8b9f6ab0...

[2]https://codelearn.me/2019/01/13/wsl-windows-toast.html


on a Mac

  say “Build Done”


on Ubuntu:

    spd-say "Build Done"


Yup.

  npm i && say 'done installing. back to work now'


I do this too soo good!


This looks really cool! I recently built something similar, but for notifying you via text message instead.

Example:

./some_script.sh; curl https://nudge.sh/YOUR_TOKEN

Just need to register your phone number at nudge.sh to get a token (don’t worry, your data is safe and not shared with anyone or used for any other purpose)


Ah, I have something like this just for `make`[0], which I've found very useful.

Running it on DEBUG so it covers all commands is an interesting idea. I'm not sure I'd want alerts on things like exiting vim/less though. I don't suppose there's some nifty heuristic for whether programs accept user input?

[0] [link redacted]


For what it's worth, Starship also does this(https://starship.rs/config/#command-duration), with the possibility to only trigger notifications for tasks running more than N seconds.


This is great. Wanted to something about this for a long time. There are more features I desire: notify me when some string pattern occurs in the log. Also I tend to walk away from desk when I run a long program. So sometimes I'd prefer more aggressive notifications such as calling my cell.


Konsole lets you turn on notifications after X seconds of activity or X seconds of inactivity, and these two options are conveniently accessible in the Show menu. I just noticed that there is also a "Monitor for Process Finishing", this must be new :-)


Some time ago I also played around with notifications when a long running task occasionally stopped for user input (like apt-get). I had some LD_PRELOAD hack that worked ok, hooking into read calls. I probably lost it, it would be nice to rediscover it again.


I just use printf \a for this.


Yep. "tput bel" is my version


My personal favorite method was to have a script sms me at the end of a long running task with the status code, last few lines if any error and how long it took to run.


For Linux equivalents, there’s Dunst with libnotify. If you want to walk away from your computer and want a notification to your phone, Pushover is great


echo [CTRL+V][CTRL+G]

(actually do the keystrokes) works for me in a pinch


tput bel




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: