Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a Irssi running within a tmux session, and am working on writing a Irssi script (extension) to send me an email when someone PMs or mentions me when I'm not attached to the tmux session.

Everything about this is quite easy, less one thing: how do you detect if the tmux session is attached?

I've gotten so far with this, and am reaching out here mainly to see if this is the correct/best way to do it, or if I should be doing it differently. Any advice is appreciated!

# Get the current session_name value from tmux
$ tmux display -p '#{session_name}'

# Running list-clients while I'm attached yields the following,
# from both the same window as well as another window:
$ tmux list-clients -t 2
(null): 2 [180x42 (null)] 

# As well, running list-clients while detached yields no output:
$ tmux list-clients -t 2

All told, I think this is the right way to go about this, but I'm certainly open to any better ways to or suggestions on how to check this.

share|improve this question
tmux ls | grep attached? – jasonwryan Nov 23 '13 at 19:55
jasonwryan: You have a fair point re: list-servers. I was using list-clients since it supported specifying which session, whereas list-sessions lists them all, meaning a bit more work in the Perl side of things. Not necessarily a bad thing, a part of me was just hoping for something simpler to be built into tmux. :) – Spikes Nov 23 '13 at 20:45
To simplify, start your Irssi session with a name, and then grep for $name and attached... – jasonwryan Nov 23 '13 at 20:46
Yeah, I can always go that route, but I'm looking to make this rather generic and toss it up on scripts.irssi.org when I'm done. Can't always expect that from everyone. Either way, should be a fairly easy thing to loop through list-sessions. (Playing with that idea now) – Spikes Nov 23 '13 at 20:59
up vote 5 down vote accepted

OK, I think I have the pieces you need, but I'll leave it up to you to string them together into a cohesive whole.

The TMUX environment variable will tell you if the current process is running under tmux or not:

<~> $ echo $TMUX

The last value (8) is the Session ID (which may or may not also be the session name). In the example above, our session ID is 8, but we don't have a session named '8':

<~> $ tmux ls
0: 1 windows (created Sat Nov 23 21:17:45 2013) [80x23]
1: 1 windows (created Sat Nov 23 21:17:45 2013) [120x34]
bar: 2 windows (created Tue Nov 26 03:05:03 2013) [120x34] (attached)
blech: 1 windows (created Tue Nov 26 03:12:46 2013) [120x34] (attached)

But we can get this info out of tmux using the -F format string:

<~> $ tmux ls -F "#{session_name}: (#{session_id})"
0: ($0)
1: ($1)
bar: ($5)
blech: ($8)

You could then use this info to see whether or not it's attached:

<~> $ tmux ls -F "#{session_id}: #{?session_attached,attached,not attached}"
$0: not attached
$1: not attached
$5: attached
$8: attached

Let me know if you need help putting it all together.

share|improve this answer
Thanks Joe. I actually came to a very similar end result based on jasonwryan's comments (before I saw your response), but since you had posted the first comment, I've accepted this to mark it as resolved. I'll also post what I ended up doing for anyone else as another answer, as if you're doing it via code, there's a slightly easier way. :) Thanks again for your time! – Spikes Dec 6 '13 at 22:05

Based on jasonwryan's advice in the comments, I ended up with the following two commands.

Show the current session name.

Since we're in a scripting language, we don't necessarily need to know the session ID.

tmux display -p '#{session_name}'

List all sessions by name, and their respective attached statuses.

session_attached is a bool, and is represented as a single digit, making it easy for most scripting languages to parse this output format.

# Show the session attached status for each session name. session_attached
# is a single digit, either 0 or 1, making it easy to distinguish 
tmux ls -F '#{session_attached} #{session_name}'

In Perl (IRSSI plugins), parsing this output format is as easy as

my ($attached, $name) = $line =~ /^(\d) (.+)$/;
share|improve this answer
I thought I had tried something like the tmux -p '#{session_name}' and it hadn't worked for me, but I guess I did something wrong. Glad you found an easier way. – Joe Casadonte Dec 8 '13 at 4:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.