Open tmux profile when VPN comes up if not open

Skip to first unread message

Jeffrey Honig

Apr 1, 2022, 9:47:03 AMApr 1
to iterm2-discuss
When my work VPN connection comes up, I would like to start tmux via a profile if it is not already connected.

I have a launch agent script that runs when the VPN status changes and if the VPN is up I would like to ensure a specific tmux connection is up.  However there are instances where the tmux connection may already be up, such as a VPN drop that is short enough not to break the existing connection.

I use tmux to different hosts, so I need a way to figure out if the specific tmux connection is up.  

I'm currently blindly launching the profile with this tmux session via AppleScript, but should convert this to python.

Could someone point me in the right direction?



George Nachman

Apr 3, 2022, 2:09:05 PMApr 3
Use the Python API. It has limited support for tmux integration but it just might be enough for what you want.

Here are some helpful links:

You received this message because you are subscribed to the Google Groups "iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

Jeffrey Honig

Apr 3, 2022, 4:02:58 PMApr 3
Using the Python API I found several methods of determining that A tmux session was active, but no easy way to match it back to a profile.

The best I can come up with is using -s SESSIONNAME on my tmux command in the profile definition and scanning profile definitions to find those.  Then I can issue a tmux command to list active sessions at compare name, like this:

    tmux_conns = await iterm2.async_get_tmux_connections(connection)
    logging.debug("%d Tmux connections", len(tmux_conns))

    found_tmux = False
    for tmux_conn in tmux_conns:
        sessions = await tmux_conn.async_send_command("list-sessions -F '#{session_name}'")
        logging.debug("sessions: %s", sessions)
        for session in sessions.split('\n'):
            if session == profile_session_name:
                logging.debug("found session: %s", session)
                found_tmux = True

    if not found_tmux:
        logging.debug("Opening profile: %s", options.profile)

You received this message because you are subscribed to a topic in the Google Groups "iterm2-discuss" group.
To unsubscribe from this topic, visit
To unsubscribe from this group and all its topics, send an email to
To view this discussion on the web visit

George Nachman

Apr 3, 2022, 5:28:23 PMApr 3
I’m not sure if it’s sufficient to solve your problem but each session that belongs to tmux integration has variables giving its tmux window pane. That’ll tell you whether it is a tmux session, although it’s not enough info to guess which tmux connection it belongs to if there are multiple. The test is:

await session.async_get_variable(“tmuxWindowPane”) != None

What would the ideal API look like to you?

Jeffrey Honig

Apr 3, 2022, 6:00:36 PMApr 3
After getting familiar with the Python API again, I think what I’m looking for is a way to get from a tux connection (as returned by iterm2.async_get_tmux_connections(connection)) back to the name of the profile that launched it.  I poked around looking for something, but couldn’t find it.  Does that exist?

I reconfigured so that each profile that launches tmux uses a unique session name, so the code currently does what I want.  It would just be nicer if I didn’t have to maintain the extra level of unique session names.

George Nachman

Apr 3, 2022, 7:37:26 PMApr 3
TmuxConnection has owning_session that gives you the Session in which `tmux -CC` was run. The tmux profile will be approximately equal to its profile. So:


The font size could be different in tmux client windows, and in the nightly build the key bindings might also differ.

If you have it set to use the “tmux” profile then it’s a different story—just get the profile named “tmux”. You can test for this with async_get_preference(iterm2.PreferenceKey.USE_TMUX_PROFILE)

Jeffrey Honig

Apr 3, 2022, 8:16:48 PMApr 3
owning_session is None when I look at it on Build 3.5.0beta5:

sessions: cloud-jump-nym2-13353-p-mac
> /Volumes/Xandr/bin/iterm2_jump(112)work()
-> for session in sessions.split('\n'):
(Pdb) str(tmux_conn.owning_session)
(Pdb) l
108         found_tmux = False
109         for tmux_conn in tmux_conns:
110             sessions = await tmux_conn.async_send_command("list-sessions -F '#{session_name}'")
111             logging.debug("sessions: %s", sessions)
112 B->         for session in sessions.split('\n'):
113                 if session == profile_session_name:
114                     logging.debug("found session: %s", session)
115                     found_tmux = True
116                     break

George Nachman

Apr 5, 2022, 12:54:47 AMApr 5
This happens if the session is buried. That’s a bug. Get version 2.3 of the iterm2 module and it should work now.

Jeffrey Honig

Apr 5, 2022, 9:53:57 AMApr 5
That helped me move forward.  Unfortunately, Automatic Profile Switching has changed the profile.  Is there a way to disable APS from switching away from the profiles I use to launch tmux?

Reply all
Reply to author
0 new messages