Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to get notification on long duration command completion in Fish

📅 2013-Jun-29 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ fish, notification ⬩ 📚 Archive

Some commands take a long time to finish. For example, compiling C++ code or updating a lot of packages can take a while. Since the command completion time is long and variable, you might switch to a different task. But, you might want to be notified when that command is completed.

One technique to get such a notification in the Fish shell is by using the CMD_DURATION environment variable. Fish sets this to the number of milliseconds taken by the last executed command. It is set only if the last command took more than 1 second to complete.

By adding few lines of code to the fish_prompt function in the ~/.config/fish/functions/ file, we can get a notification. I like to trigger a notification using the notify-send program, which displays a notification in Unity:

### Notify on long command completion

# If commands runs >= 10 seconds, notify user on completion
    if test $CMD_DURATION -gt (math "1000 * 10")
        set secs (math "$CMD_DURATION / 1000")
        notify-send "$history[1]" "Returned $status, took $secs seconds"

If you want to customize the display properties of the notification prompt, that can be done as explained in this post. Note:

Reference: Weird format of CMD_DURATION

Tried with: Fish 2.2b1 and Ubuntu 14.04