MacOS — Trigger Notification Center when long running commands finishes

Before getting into the main subject, be aware that this article has been first write for the Blog of Antistatique — Web Agency in Lausanne, Switzerland. A place where I work as Full Stack Web Developer.

Feel free to read it here or check it out there: https://antistatique.net/fr/node/343

Retrieve all the code on my Gist: https://gist.github.com/Sudei/ad0c1330966522c05f504d71d29f675b

Happy reading.

As a developer, I often run commands that take a while to finish:

  • git pull
  • composer install
  • npm install

Considering that I am not a very patient person, I get really bored waiting for these commands to finish, so during that time I usually switch to some other occupation such as:

  • Checking Twitter/Tech/Hacking websites,
  • Making & (sometimes) drinking coffee,
  • Playing T-Rex Runner — The hidden endless running game of Chrome.

15 minutes later, I finally come back to my terminal with a cold coffee and I discover that I forgot to cd in the correct folder. Repeat for another 15 minutes.

It doesn’t exist a better way ?

Work is hard. Distractions are plentifull. And time is short
 — Adam Hochschild, American author, journalist, and lecturer

Solution N°0 — The Junior Professor

Waiting by watching some Gifs right on the terminal with Gifi. One of my favourite solutions but not enough “cool” for some people.


Solution N°1 — The Middle-Earth Paradigm

Making all tools faster and developers more patient. That would work in a parallel universe where magic exists, maybe.


Solution N°2 — The Luminous Fish Effect

Use a short script that uses terminal-notifier & append it to each command such as:

$ git pull repo@github.com | notify-me

That’s pretty cool and do the job but … I’m lazy and I don’t want to append anything to my commands or predict if my command is a long-running command or not before even running it.


Solution N°3 — The Friendship Algorithm

What about trying another approach ? Instead of trying to detect long-running commands, we could simply send a notification when the CLI is not in the foreground. Mmmh, sounds good but is that possible ? With iTerm & OSX 10.9+, you can do this !!

1.Create a new command named notify

$ touch /usr/local/bin/notify && chmod +x /usr/local/bin/notify
$ vim /usr/local/bin/notify

2. Copy and past this system script inside

#!/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

3. Add a custom function nammed f_notifyme and expose it to iTerm using PS1

vim ~/.zshrc

4. Copy and past the function

function f_notifyme {
LAST_EXIT_CODE=$?
CMD=$(fc -ln -1)
# No point in waiting for the command to complete
notify "$CMD" "$LAST_EXIT_CODE" &
}
export PS1='$(f_notifyme)'$PS1

5. We did it ! Finally, reload the source file of your terminal source ~/.zshrc and enjoy !

Enjoy your new notification cli system !

All images copyright of their respective owners.
Big thanks to @Antistatique for the review & @gratisography for images.

Show your support

Clapping shows how much you appreciated Kevin Wenger’s story.