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.
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)
visual-bell on
means visual message only. If you do visual-bell both
, then you both hear the bell and get a visual message.
command-prompt
: stackoverflow.com/questions/32033091/…
Dec 26, 2020 at 14:33
Bell in window X
, just to trigger bell?
Feb 23, 2021 at 11:13
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 :
.
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
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
(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
$ 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
monitor-content
appears to have been removed. But a more robust technique anyway might be something like my-command; tmux display-message "We're done here!"
Aug 15, 2016 at 17:19
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.
set-option -g bell-action other
set in tmux, or else you get a bell on every command and have to wait until the bell finishes. If you ever use your shell without tmux, then there is also a bell on every command.
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
For future reference, people can also checkout the tmux-notify plugin, created by ChanderG, which was designed for this purpose.
Disclaimer: I'm a contributor to this plugin. Feel free to check it out and open an issue/pull request or feature request if you find a bug or see something that is missing.
there are two options:
set -g visual-activity on
setw -g monitor-activity on
have you tried setting them in your tmux.conf?
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)
Building on @psp940's answer (where StackOverflow won't let me add a comment):
precmd () {
echo -n -e "\a" >$TTY
}
If you're using powerlevel10k's instant prompt feature, redirecting the bell directly to the terminal avoids a warning about pre-init console output. See also here.
Adding onto the answers from @wump and @psp940; I've found that the simplest way to be alerted any time a command completes is to put a bell in your PS1
:
in tmux.conf
set-window-option -g visual-bell on
set-window-option -g bell-action other
In your .bashrc
:
# ring bell every time a command completes
export PS1+=$'\[\a\]'
I'm not sure whether the +=
and $''
are bash-isms, but the principle will work in any shell.
The surrounding \[
+\]
tell your shell that the characters between them are zero-width