179

A notification center notification would be ideal but growl, bounce dock, sound, etc would be fine, too (or if this can only be done in Terminal.app I'd be willing to switch back). Is there an option somewhere in iTerm to turn on notifications or is it something I'm supposed to type at the end of a command in the terminal? If the latter, is it possible to add an alert once process has started (for example if I realize it's going to take longer than I initially expected, I'm bad at guessing).

truth1ness
  • 4,831
  • 5
  • 21
  • 19

7 Answers7

330

Notify on an already running process:

  • EditMarks and AnnotationsAlertsAlert on next mark
  • or Shortcut: A

Spooky eye! : iTerm will literally keep an eye (on top right corner) of your terminal. Once the command is finished, it will issue a notification.


Requirement: Shell Integration:

  • iTerm2Install Shell Integration
  • NOTE: integration will not issue notifications until iTerm2 is restarted.

Example Use Case:

We launched a command, underestimated completion time, and we don't want to cancel or just wait for completion.

Paschalis
  • 11,929
  • 9
  • 52
  • 82
  • 20
    After installing shell integrations you might want to go to iTerm2 -> Preferences -> Profiles -> Terminal , scroll down & uncheck "Show mark indicators", I did not like this shell integration. You might also want to Edit -> Marks and Annotations -> Alerts -> Post notification if you want the os notifications instead of an alert style popup. – Charles L. Nov 21 '17 at 23:45
  • @simonhamp I suggest using triggers (https://www.iterm2.com/documentation-triggers.html) for buried sessions – phirschybar Dec 26 '18 at 14:33
  • 3
    @dwanderson Did you ever get this working with tmux? This would be so helpful – Lucas Feb 28 '19 at 01:08
  • 2
    @Lucas there is a line in the docs that says that shell integration doesn't work with tmux or screen https://iterm2.com/documentation-shell-integration.html – Rich Steinmetz Mar 10 '20 at 07:54
  • 30
    This eye scares me!! (╯‵□′)╯︵┻━┻ – WTIFS Jun 02 '20 at 07:21
  • The alert does not work on ssh sessions for me. Anyone experience the same? – Shamal Karunarathne Nov 23 '20 at 18:00
  • 2
    @ShamalKarunarathne Have you installed the shell integration scripts in the machines you are SSHing to? – GregL Dec 04 '20 at 04:16
  • 1
    @GregL Thank you it worked. Didn't know I had to install the integration in SSH hosts as well. – Shamal Karunarathne Dec 05 '20 at 08:22
83

And you can always use the say command.

Usually when you are running a long process inside the terminal and want to get updated you can simply use this command to speak out things like done or error or bazinga.

mvn clean install; say done 

This command builds a java spring app, and takes a long long time, and it will speak out done after the process is complete.

Farhan Salam
  • 1,257
  • 11
  • 16
64

You can add any one of the following after any command, with a semi-colon in between the command and it:

afplay /System/Library/Sounds/Ping.aiff -v 2

osascript -e 'beep 3'

tput bel

or, if you like Notification Centre

osascript -e 'display notification "Lorem ipsum dolor sit amet" with title "Title"'

You can also make an alias in your profile, called notify and add that at the end of your command. So, in your login profile

alias notify="tput bel"

then

sleep 10; notify

Or, if you started your command and it is "hanging", just type notify and hit Enter and it will run your notify alias at the end, when the command has finished, e.g.

sleep 20

# wait 5 seconds before realising this will take 20 seconds
notify<Enter>
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Hmm, with this command `osascript -e 'display notification "Task finished" with title "Terminal"'` I seem to be getting this error `2015-05-04 12:34:23.227 osascript[30467:1130887] Error loading /Users/mmarotta/Library/ScriptingAdditions/List & Record Tools.osax/Contents/MacOS/List & Record Tools: dlopen(/Users/mmarotta/Library/ScriptingAdditions/List & Record Tools.osax/Contents/MacOS/List & Record Tools, 262): no suitable image found. Did find:` – truth1ness May 04 '15 at 16:36
  • part two of error `/Users/mmarotta/Library/ScriptingAdditions/List & Record Tools.osax/Contents/MacOS/List & Record Tools: no matching architecture in universal wrapper osascript: OpenScripting.framework - scripting addition "/Users/mmarotta/Library/ScriptingAdditions/List & Record Tools.osax" declares no loadable handlers.` – truth1ness May 04 '15 at 16:37
  • Are you on a system older than Mavericks or Yosemite? – Mark Setchell May 04 '15 at 16:45
  • Nope, Yosemite 10.10.3 – truth1ness May 04 '15 at 16:53
  • Oh, that's odd. Can you try something simpler like `osascript -e 'display notification "Hello"'` - by specifically copying and pasting from here. – Mark Setchell May 04 '15 at 17:03
  • Result: (testenv3)testenv3 > osascript -e 'display notification "Hello"' 2015-05-04 19:56:53.205 osascript[57251:2660824] Error loading /Users/mmarotta/Library/ScriptingAdditions/List & Record Tools.osax/Contents/MacOS/List & Record Tools: dlopen(/Users/mmarotta/Library/ScriptingAdditions/List & Record Tools.osax/Contents/MacOS/List & Record Tools, 262): no suitable image found. Did find: /Users/mmarotta/Library/ScriptingAdditions/List & Record Tools.osax/Contents/MacOS/List & Record Tools: no matching architecture in universal wrapper – truth1ness May 04 '15 at 23:57
  • part two: osascript: OpenScripting.framework - scripting addition "/Users/mmarotta/Library/ScriptingAdditions/List & Record Tools.osax" declares no loadable handlers. – truth1ness May 04 '15 at 23:57
  • http://stackoverflow.com/users/2836621/mark-setchell this is great!! Is there a way to not open apple script editor when I click on the notification by accident? – lacostenycoder Dec 13 '16 at 16:48
  • 1
    `tput bel` is great because it also works when connected to another machine via e.g. ssh. – Ben Blank Nov 06 '19 at 18:39
  • to chain osascript notification and beep: `alias notify="osascript -e 'display notification \"execution finished\"'; osascript -e 'beep 5'"` – Rich Steinmetz Mar 24 '20 at 07:29
21

iTerm2 supports Growl notifications. You can turn it on in each profile settings.

Select a profile in Preferences…->Profiles. Then in Terminal tab there is an option Enable Growl Notifications.

Remember to also enable iTerm notifications in Growl preferences.

If you want to get notification for a given process you could try to experiment with Triggers. You define triggers in Advanced tab in a profile settings. In this way you may assign a Growl notification to a particular output of your process (regexp).

You could for example do:

$ mycommand; echo "end-of-my-process"

And connect trigger to "end-of-my-process" message.

Update

Read more about triggers on iTerm2.com.

baf
  • 4,531
  • 1
  • 21
  • 24
  • So am I correct that there is no global way to get an alert for any finished process because I need to have run the script once to know what it writes at the end of the process (and I have to create one for every script I run)? For example, the last line for a youtube-dl process is `[download] 100% of 40.13MiB in 00:13` but this line is repeated for every file it finishes. What would I put in for my 'end-of-my-process' string? Could I make a trigger for when my default shell prompt `~ >` shows up again? – truth1ness May 03 '15 at 22:42
  • @truth1ness I don't think there is a generic way to be informed about finished process. `iTerm` sends notifications for new terminal output, for end of terminal output. This can work for some processes. The way to work for all processes is what I suggested. "end-of-my-process" message can be any unique string. Unique it such way that it will not be printed by your process. Could be literally "end-of-my-process" text. You must enter exactly same string in the trigger definition and attach a notification to it. I don't know if it will work with shell prompt. You must try it yourself. – baf May 04 '15 at 05:33
  • Ah, I misunderstood, I got it now. I was able to get an alert to pop up with the echo output "end-of-my-process". However, I also get an alert when I actually issue the echo command before the process starts. How would I tell iTerm to ignore the string when I actually issue the command and only look at the echo'd string? From the help file I tried prepending the trigger regex with \0 ("The entire value matched by the regular expression.") but it still executes the trigger twice. – truth1ness May 04 '15 at 16:40
  • 1
    @truth1ness Be creative, for example use `echo "end-of-""my-process"` with quotes inside. It will match "end-of-my-process" text only when process finished. – baf May 04 '15 at 16:52
12

There is an OSS tool called noti.

You can easily install it with brew install noti and start using it just by prefixing your command with noti like noti sleep 3.

dfang
  • 1,366
  • 15
  • 41
junjian.xu
  • 123
  • 1
  • 5
7

You can also use terminal-notifier which use mac os system notifications. To install it via Home brew just:

$ brew install terminal-notifier

Then if you want to display notification when your job/process is done use something like this

$ <your job/process command> && echo 'Completed' | terminal-notifier -sound default

And this display like this:

enter image description here

You can also change sound and icon of notifications. More info in github repo: https://github.com/julienXX/terminal-notifier

MaSza
  • 435
  • 7
  • 22
7

Install the iTerm2 shell integration

curl -L https://iterm2.com/shell_integration/install_shell_integration_and_utilities.sh | bash

Execute your command and concatenate the attention app, e.g.

./task && ~/.iterm2/it2attention once

It'll cause the iTerm app to bounce it's icon once the job is complete.

You also have other attention options:

$ .iterm2/it2attention -h
Usage:
  it2attention start
    Begin bouncing the dock icon if another app is active
  it2attention stop
    Stop bouncing the dock icon if another app is active
  it2attention once
    Bounce the dock icon once if another app is active
  it2attention fireworks
    Show an explosion animation at the cursor
leonardo
  • 1,686
  • 15
  • 15