3

Is there a way to get notified when some terminal task is finished "in the background" in the terminal? I need to get notified when npm run build is over which can take up to 15 mins. I've considered using iTerm2 as well as adding terminal-notifier commands to my package.json but none are suitable enough.

Can this be achieved by some native OSX features?

  • Sorry, this is an answer, but I seem only to be able to comment:

    At least on macOS Monterey one can issue from the terminal or a shell script:

     *** osascript -e 'display notification "text" with title "text" sound name "default"'***
    
    

    To allow such notification one must go to "System preferences–Notifications & Focus–Script Editor" and select "Allow Notifications", and "Alerts" (display for three seconds) or "Banners" (display until closed). For silent ones, omit 'sound name "default"'.

    – Kristjan Jonasson Sep 17 '22 at 11:37
  • Thank you @KristjanJonasson - could you post your answer as an answer there, please? This thread is closed to new answers and you are correct that you have a proper answer and not a comment. – bmike Sep 17 '22 at 14:14
  • Have done so. See now my answer to a similar question – Kristjan Jonasson Sep 18 '22 at 21:26

1 Answers1

4

The simplest way would be to invoke a notification using Applescript after a given terminal command like so:

npm run build && osascript -e 'display notification "Complete" with title "npm run build"'

The next level would be to create a custom script in package.json:

"scripts": {
  "build-notify": "npm run build && osascript -e 'display notification \"Complete\" with title \"npm run build\"'"
}

Another option would be to create a custom alias to run the whole thing: alias build="npm run build && osascript -e 'display notification \"Complete\" with title \"Build complete\"'"
Then run it like a regular terminal command: build

If you don't mind the audio, you could use the say command instead:
npm run build && say done.

There are also some tools that abstract this a bit, like https://github.com/julienXX/terminal-notifier and maybe many more.

gedijedi
  • 176
  • 4