Alert in tmux when a process completes

2020-05-26 04:43发布

问题:

Can I set tmux to trigger an alert in a non-active window when a process completes?

For example: I start a long build process. I would like to be notified when it completes, not each time it prints a status.

回答1:

I'm surprised this answer hasn't been given yet: You can use the tmux window setting visual-bell for this. With bell-action you can then configure whether you want to see bells for the current window only, or for non-current window only (other). Personally I prefer the second, as you won't see noise generated by the shell, and you probably don't care about the notification if it's in the current window.

set-window-option -g visual-bell on
set-window-option -g bell-action other

When a process generates a bell, tmux will highlight the the title of the window that rings the bell as well as show a "Bell in window X" notification.

Then ring the bell at the end of the process. E.g.:

make; echo -e '\a'

(or && || instead of ; if you want to ring only on success or failure respectively)



回答2:

There 3 solutions I know, none really ideal. You may put those commands in your ~/.tmux.conf or just run them directly as Tmux command via Ctrl-B :.

  1. Monitor and alert whenever the output changes (you may then redirect output somewhere else so that output changes only after the command is complete):

    :set -g visual-activity on
    :setw -g monitor-activity on
    
  2. Monitor and alert whenever the output did not change for a while (30 seconds here):

    :set -g visual-silence on
    :setw -g monitor-silence 30
    
  3. (deprecated and someday replaced by a better option) Monitor and alert whevener the output contains a string matching given pattern (and possibly run your command like my-command; echo foobar):

    :set -g visual-content on
    :setw -g monitor-content foo*bar
    
  4. $ some-command; tmux display-message "Task 1 done". However the message will only show for a short duration defined via :set -g display-time 4000.

If you keep the visual-* to off (default), it'll only highlight the name of the window in which the alert occurred without showing a global alert status message.

For details about each of these settings, see tmux man page

Updated (thanks for Steven Lu)



回答3:

I finally found a solution that works for me. I use zsh for my shell, which has a feature called "Hook Functions" -- shell functions that execute on certain actions: http://zsh.sourceforge.net/Doc/Release/Functions.html.

It's likely other shells have a similar feature.

The hook function I use is precmd, which is executed each time the prompt is shown. ie, when a command has just finished running.

In my .zshrc:

precmd () {
  echo -n -e "\a"
}  

This sends a bell to tmux, which causes it to highlight just the window that the command was running in.

If you are already focused on that tmux window, this does nothing because the bell is immediately cleared.

The benefit of this approach is that it does not trigger on all visual activity. It only triggers when a command completes.



回答4:

you can wrap your running script with a && bash derivative which will call a tmux command to notify you. using && means you will only get notified when the script exits with error code 0. if you want it to notify you anyway, just use ;

as for the tmux commands to wrap the script, have a look at those bunch, they should suffice select-window split-window -h 'exec echo...' send-keys



回答5:

there are two options:

set -g visual-activity on
setw -g monitor-activity on

have you tried setting them in your tmux.conf?



回答6:

As a good workaround, you can use: https://github.com/tcreech/tmux-notifications

You simply do: $ command ; tmux-notify

You will get a nice notification in the status-bar (if enabled)



标签: tmux