1

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:

#!/bin/sh

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
done

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 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 at 23:04
1

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.

  • 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 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 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 at 16:02

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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