I'm in the process of making a x11vnc launching script that would fix the dreaded black screen that prevents you from using VNC until reboot. So far, I've managed to find a workaround by using the newest /run/sddm/* file and changing :0 to n-1 of files in there accordingly. This is guesswork though and it will only work as long as the user switching only adds a display session to the end and then removes it. Once you try switching back using an item that is not the last, the problem returns because I have no way of telling which display is being used. My progress so far:


while true; do
lso=`ls -t /run/sddm/{* | grep -m1 -`
cnt=$((`ls /run/sddm/{* | grep -c -`-1))
/usr/bin/x11vnc -auth $lso -once -noxdamage -repeat -rfbauth /home/USER/.vnc/passwd -rfbport 5900 -shared -display :$cnt
usleep 400000

The highlight is that I got rid of the -forever and -loop to have it reload the server and supply it with fresh display config after every connection.

I need the current display, such as :0 or :1 to find the correct X cookie file (I can do that if I know the display number) and the correct output to have it show the proper monitor picture.

Running on Kubuntu 18.04 with all updates.

  • serverfault.com/a/675540/149828 cat /sys/class/tty/tty0/active – basin Jan 11 '19 at 22:11
  • @basin this is certainly answering the question from the title, but the OP's problem seem to be how to find the display number (:0, :1) – Uncle Billy Jan 11 '19 at 23:04

If you want to find the display number of the Xorg server running in the current linux vt (no Xvfb or xnest), then maybe this will do (completing @basin's comment)

pgrep -at "$(cat /sys/class/tty/tty0/active)" Xorg | grep -Po ' \K:[0-9]+'

The -t tty option of pgrep will select processes which have tty as controlling terminal, and the -a option will direct it to print the whole command line; the second grep will extract the display number from it.

Of course, that won't print anything if the user had switched to a text console via Ctrl-Alt-Fn.

| improve this answer | |
  • 1
    This is clever, but if I may suggest; you might want to add commentary as to how this works (searches all open files for anything matching the active tty, look at the command line of those processes, limit it to Xorg, return the display) so that people looking at this in the future can understand and learn from it. – Stephen Harris Jan 12 '19 at 0:03
  • The main machine is off and my local one complains Directory or file doesn't exist. What's stranger is that when I run it via sudo, it writes one such error, but under clean root, it writes 2 errors of the same kind. It never returns any display number, unfortunately. When the machine comes back on, I'll test your suggestion there. – Zdenek Jan 12 '19 at 20:04
  • I've updated to a much better solution that shouldn't require root or sudo, and also won't be fooled by an Xserver started manually from a shell in a vt. If it doesn't work, try it by pieces (first cat /sys/class/tty/tty0/active, the pgrep -t <that_tty>, etc. – Uncle Billy Jan 13 '19 at 16:02

I finally figured it out. Props to Uncle Billy for pointing me in the right direction (although his second edit still didn't work for me as stated).


while true; do
lso=/run/sddm/`pgrep -at "$(cat /sys/class/tty/tty0/active)" Xorg | grep -Po '\{[0-9a-f\-]{36}\}'`
cnt=`cat -v $lso | grep -Po '\^[A-C]{1}[0-9]{1,3}\^@\^RMIT' | grep -Po '[0-9]{1,3}'`
/usr/bin/x11vnc -auth $lso -once -noxdamage -repeat -rfbauth /home/USER/.vnc/passwd -rfbport 5900 -display :$cnt
usleep 400000

Here's how it works: The variable lso needs to contain the current X-cookie file's full path. Here is where Uncle Billy's suggestion comes in. While there is no display number in the pgrep line output, the GUID-style X-cookie is. That's what I crudely grep without considering the exact location of the hyphens in the hex string surrounded by parentheses.

The cnt variable needs to contain the display number. I extract that from the X-cookie itself. The cat -v turns binary characters into caret-escaped control characters. I then match these binary characters with what surrounds the display number in the X-cookie file. The [A-C]{1} part is actually the length field for the display number. A means one character, B two etc. I can't parse this as intended, but luckily, the regexp used can do without that. The script therefore supports up to 999 displays which should be plenty.

One disadvantage is that on different systems, the zero-length strings represented by ^@ could be populated breaking this script. If anybody knows what to do better, please tell me.

Also, I'd rather detect the display number without extracting it from the X-cookie because the X-cookie is not present on some systems making this script less universal.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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