Skip to content

Instantly share code, notes, and snippets.

@mieky
Last active July 26, 2016 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mieky/31a327bb0aa05c4001b5b90fbbcd03a8 to your computer and use it in GitHub Desktop.
Save mieky/31a327bb0aa05c4001b5b90fbbcd03a8 to your computer and use it in GitHub Desktop.
notify-on-completion for Vagrant

Alex Kotliarskyi wrote a great little script to show OS X desktop notifications when a long-running terminal command finishes (and is not in focus).

Making it work with a Vagrant virtual machine turned out to be straightforward. These instructions assume zsh as shell, bash might work similarly.

  1. Following Alex's instructions, install the notifyme script on OS X, in e.g. ~/bin/notifyme (assuming ~/bin is in $PATH, remember to chmod u+x notifyme)
  2. (Vagrant) Install Ruby on the VM: sudo apt-get install ruby
  3. (OS X) Install the vagrant-notify plugin: vagrant plugin install vagrant-notify
  4. (OS X) Restart the virtual machine: vagrant halt & vagrant up
  5. (OS X) Alias our script under a name which the plugin knows: ln -s ~/bin/notifyme ~/bin/notify-send
  6. (Vagrant) Append to the end of the ~/.zshrc in your Vagrant box:
function f_notifyme {
    LAST_EXIT_CODE=$?
    CMD=$(fc -ln -1)
    # No point in waiting for the command to complete
    notify-send "$CMD" "$LAST_EXIT_CODE" &
}

export PS1='$(f_notifyme)'$PS1

Fire up a new zsh shell, or do a source .zshrc, and you should get notifications of finished shell commands on your desktop!

#!/usr/bin/env osascript
on run argv
tell application "System Events"
set frontApp to name of first application process whose frontmost is true
if frontApp is not "iTerm2" then
set notifTitle to item 1 of argv
set notifBody to "succeded"
set errorCode to item 2 of argv
if errorCode is not "0"
set notifBody to "failed with error code " & errorCode
end if
display notification notifBody with title notifTitle
end if
end tell
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment