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/fish_prompt.fish 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|
|if test $CMD_DURATION -gt (math "1000 * 10")|
|set secs (math "$CMD_DURATION / 1000")|
|notify-send "$history" "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.
- You get a notification on your computer even if the command you are running is on a remote machine over SSH! For this to work, the Fish shell on the remote computer should have the above configuration files and you must have connected to it with X11 Forwarding enabled. A useful trick is to add the hostname of the computer to your notification message so that when it pops up you know which remote computer finished its command.
You need Fish v2.2 or later for this technique to work. In earlier versions
CMD_DURATIONis returned as a string value with different suffixes that is difficult to parse.
Reference: Weird format of CMD_DURATION
Tried with: Fish 2.2b1 and Ubuntu 14.04