20

I have created a shared socket for tmux to use.

tmux -S /tmp/pair

Then I have a 2nd user attach to the socket.

tmux -S /tmp/pair attach

How do I tell from the first session, the one that created the socket, tell that the 2nd user is connected?

I'm guessing it would be something from the lsof command.

Jason
  • 3,736
  • 5
  • 33
  • 40

3 Answers3

25

You can use the list-clients command. By itself, it displays all clients connected to the server. If you specify a session with the -t option, it shows clients attached to the named session.

chepner
  • 497,756
  • 71
  • 530
  • 681
14

Like another user said in comments <prefix> D will list all the sessions but keep in mind that pressing enter will detach that client (which may or may not be what you want)

Shubham Chaudhary
  • 47,722
  • 9
  • 78
  • 80
0

The answers given (Ctrl-b D or Ctrl-b : list-clients RET) will give you a list of clients with their (virtual) tty terminal and window size, but does NOT tell you the associated username. From outside of a tmux session, you can also use "tmux -S /tmp/pair list-clients"

One method to list actual users that are connected is to run lsof on the named socket, ie: "lsof /tmp/pair"

Or you can use the 'who' command to view a listing of users associated with the virtual terminals. If you are just differentiating between your own connections, you can use an alis to filter that: "who | awk '{print $2,$NF}' |grep -v '(:[0-9]'"

Here's a quick Perl script that puts the tmux and 'who' outputs together. For usage with shared sockets, pass the socket file as the sole argument, ie: "tmux_ls.pl /tmp/shared":

my $sock="";
$sock = "-S ".$ARGV[0] if $ARGV[0];

my $clients = `tmux $sock list-clients`;

# Use open to loop like a file
open my $fh, '<', \$clients or die $!;

printf("%-10.10s\t%-10.10s\t%-10.10s\t%-16.16s\t%s\n", "Session", "TTY", "Username", "Timestamp", "Origin");
while(<$fh>) {
    my @cols = split(' ');
    my ($tty) = $cols[0] =~ /^\/dev\/(pts\/\d+)/;
    my $session = $cols[1];
    my $who = `who | grep $tty`;
    my @whoc = split(' ',$who);

    printf("%-10.10s\t%-10.10s\t%-10.10s\t%-16.16s\t%s\n", $session,$tty,$whoc[0], "$whoc[2] $whoc[3]", $whoc[4]);
}
Digicrat
  • 581
  • 5
  • 13