Nathan Grigg

Monitor long-running commands: integrations

This is the fourth and last in a series of posts describing the system I built to monitor long-running commands.

The third post explained my mon script, which together with my twait script from the second post, lights up an LED when a command in a tmux pane or iTerm window completes.

If I want to monitor a command, I use mon twait ID, where ID is the pane or window ID. Or I can run another command only if the first command is successful, by writing twait ID && ./other_command.

Of course it would be annoying to type the window ID manually, so I have two keyboard shortcuts. One to start mon twait ID in a separate window that closes automatically when it is done. The other to open a new window with the text twait ID && followed by a space.

Tmux integration

Tmux can be scripted through the use of unix-like commands, with arguments and options. These can either be run from the command line or assigned keyboard shortcuts.

I use Ctrl-A plus uppercase E for the first.

bind E run "tmux split-window -l 5 \"mon twait #D\""

This splits to form a new window, 5 lines high, and runs mon twait. The #D parameter in a tmux run command gets translated into the pane ID.

I use Ctrl-A plus lowercase E for the second.

bind e run "tmux split-window; tmux send-keys \"twait #D &&\""

iTerm integration

You can do very similar things with iTerm using AppleScript.

To open the monitor in a temporary pane, I use this script. It is possible to control the height of the new pane, but I haven’t figured it out yet.

1 tell application "iTerm"
2   tell current session of current window
3     set session_id to id
4     split horizontally with default profile ¬
5         command "mon twait " & session_id
6   end tell
7 end tell

I prefer new windows over tabs or split windows, so I use this script to open a new window prepped to run a follow-up command.

1 tell application "iTerm"
2     set session_id to id of current session of current window
3     set new_window to create window with default profile
4     set new_session to current session of new_window
5     tell new_session
6         select
7         write text "twait " & session_id & " && " without newline
8     end tell
9 end tell