66"""
77import logging
88import os
9+ import shutil
10+ import subprocess
911import typing as t
1012
1113from libtmux .common import tmux_cmd
@@ -115,6 +117,42 @@ def __init__(
115117 if colors :
116118 self .colors = colors
117119
120+ def is_alive (self ) -> bool :
121+ """If server alive or not.
122+
123+ >>> tmux = Server(socket_name="no_exist")
124+ >>> assert not tmux.is_alive()
125+ """
126+ try :
127+ res = self .cmd ("list-sessions" )
128+ return res .returncode == 0
129+ except Exception :
130+ return False
131+
132+ def raise_if_dead (self ) -> None :
133+ """Raise if server not connected.
134+
135+ >>> tmux = Server(socket_name="no_exist")
136+ >>> try:
137+ ... tmux.raise_if_dead()
138+ ... except Exception as e:
139+ ... print(type(e))
140+ <class 'subprocess.CalledProcessError'>
141+ """
142+ tmux_bin = shutil .which ("tmux" )
143+ if tmux_bin is None :
144+ raise exc .TmuxCommandNotFound ()
145+
146+ cmd_args : t .List [str ] = ["list-sessions" ]
147+ if self .socket_name :
148+ cmd_args .insert (0 , f"-L{ self .socket_name } " )
149+ if self .socket_path :
150+ cmd_args .insert (0 , f"-S{ self .socket_path } " )
151+ if self .config_file :
152+ cmd_args .insert (0 , f"-f{ self .config_file } " )
153+
154+ subprocess .check_call ([tmux_bin ] + cmd_args )
155+
118156 def cmd (self , * args : t .Any , ** kwargs : t .Any ) -> tmux_cmd :
119157 """
120158 Execute tmux command and return output.
@@ -207,7 +245,10 @@ def list_sessions(self) -> t.List[Session]:
207245 @property
208246 def sessions (self ) -> t .List [Session ]:
209247 """Property / alias to return :meth:`~.list_sessions`."""
210- return self .list_sessions ()
248+ try :
249+ return self .list_sessions ()
250+ except Exception :
251+ return []
211252
212253 #: Alias :attr:`sessions` for :class:`~libtmux.common.TmuxRelationalObject`
213254 children = sessions # type: ignore
@@ -348,7 +389,7 @@ def _update_panes(self) -> "Server":
348389 return self
349390
350391 @property
351- def attached_sessions (self ) -> t .Optional [ t . List [Session ] ]:
392+ def attached_sessions (self ) -> t .List [Session ]:
352393 """
353394 Return active :class:`Session` objects.
354395
@@ -357,19 +398,22 @@ def attached_sessions(self) -> t.Optional[t.List[Session]]:
357398 list of :class:`Session`
358399 """
359400
360- sessions = self ._sessions
361- attached_sessions = list ()
401+ try :
402+ sessions = self ._sessions
403+ attached_sessions = list ()
362404
363- for session in sessions :
364- attached = session .get ("session_attached" )
365- # for now session_active is a unicode
366- if attached != "0" :
367- logger .debug (f"session { session .get ('name' )} attached" )
368- attached_sessions .append (session )
369- else :
370- continue
405+ for session in sessions :
406+ attached = session .get ("session_attached" )
407+ # for now session_active is a unicode
408+ if attached != "0" :
409+ logger .debug (f"session { session .get ('name' )} attached" )
410+ attached_sessions .append (session )
411+ else :
412+ continue
371413
372- return [Session (server = self , ** s ) for s in attached_sessions ] or None
414+ return [Session (server = self , ** s ) for s in attached_sessions ] or []
415+ except Exception :
416+ return []
373417
374418 def has_session (self , target_session : str , exact : bool = True ) -> bool :
375419 """
0 commit comments