8

The situation is as follows.

I have a multiuser desktop machine with Debian Linux 6.0 and an ATI videocard with one monitor connected. I have root access to it. There can be several KDE sessions started, like this:

$ w
 21:51:30 up ? days,  4:22,  ? users,  load average: 1.72, 1.68, 1.67
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
u1       pts/0    :0               Sat18    4days  0.00s 11.68s kdeinit4: kded4 [kdeinit]                      
u2       pts/5    :1               Mon17    2days  0.00s  6.65s kdeinit4: kded4 [kdeinit]                      

So, two virtual terminals are in use, tty7 and tty8:

$ ps aux|grep /usr/bin/[X]
root      2944  3.1 12.4 670040 1019904 tty7   Ss+  Aug27 187:52 /usr/bin/X :0 vt7 -br -nolisten tcp -auth /var/run/xauth/A:0-??????
root      5507  0.9  3.7 425136 309676 tty8    Ss+  Aug29  29:38 /usr/bin/X :1 vt8 -br -nolisten tcp -auth /var/run/xauth/A:1-??????

But of course only one of them is active at any given moment, i.e. displayed on the monitor. Someone sitting at the keyboard can switch between them using Ctrl+Alt+F[78]

So, I connect via ssh from a remote host. I need to know which X DISPLAY is active now. Is it possible? I have googled all over the place and can't find the answer.

vadipp
  • 449
  • 2
  • 4
  • 12
  • http://www.spinics.net/lists/linux-api/msg01144.html seems to be a rejected patch.. – kagali-san Aug 31 '11 at 16:12
  • @mhambra I'm not quite familiar with Linux kernel patch acceptance process, but how can we determine that it's really rejected, and not just forgotten? – vadipp Aug 31 '11 at 16:48
  • @mhambra Anyway, thanks a lot. Maybe I'll have to rebuild my kernel to accomplish this task, but I would be very-very glad to find another solution... Maybe some coding, but without touching the kernel. – vadipp Aug 31 '11 at 16:50
  • 1
    just use fgconsole, as Pablo said; checked with ssh login to my box, it works – kagali-san Aug 31 '11 at 18:04

2 Answers2

17

Alternatively, if you do not want to use sudo to figure out what the current tty is, you can use Linux-specific sysfs entry:

$ cat /sys/class/tty/tty0/active
tty8
Dmytro Sirenko
  • 271
  • 2
  • 4
10

fgconsole is what you are looking for

http://linux.die.net/man/1/fgconsole

Pablo Martinez
  • 2,326
  • 16
  • 13
  • Sorry, but it does not do what I want. It just prints `Couldnt get a file descriptor referring to the console`. Any ideas of what might be the reason? – vadipp Sep 01 '11 at 14:21
  • 2
    I was stupid enough to not use sudo :) With sudo, it works like a charm. Thanks a lot, problem solved! – vadipp Sep 01 '11 at 14:29