Run startx in the background without switching to the new virtual terminal

consoleterminalx-server

startx is able to create a new X server in a new virtual terminal. However, even if I run it in the background, ie. sudo startx &, it still switches to the new virtual terminal automatically. Is it possible to create a new virtual terminal while still staying in the old one?

Also, how can I know the file name of the new terminal? (Previously, I can use tty to check in the new window, but how can I achieve the same thing in the old one?)

Best Answer

Assuming that you're using X.org, this doesn't seem to be possible. The X server supports an option called -novtswitch but that only applies when the X server exits, not when it starts. Given the discussion in Fedora bug #246267, it seems that this option also applied when starting, but this was removed because it caused the X server to crash on a lot of hardware: the X server needs to access the video hardware when it starts.

The next best thing would be to allow a short flicker to another vt and switch back. On Linux, you can use openvt to run a command in a new virtual terminal and chvt to change back.

Create a script ~/.xinitrc.chvt containing

#!/bin/sh
echo "New X session running on vt$X_FGCONSOLE" >"$ORIGINAL_TTY"
chvt "$ORIGINAL_FGCONSOLE"
exec ~/.xinitrc

and run

ORIGINAL_FGCONSOLE=$(fgconsole) ORIGINAL_TTY=$(tty) openvt -s -- sh -c 'export X_FGCONSOLE=$(fgconsole); startx ~/.xinitrc.chvt -- vt$X_FGCONSOLE'

(The separate script ~/.xinitrc.chvt as opposed to sh -c … is needed because startx messes up arguments containing wildcards, and you need to pass the absolute path due to another quirk of startx. Alternatively, call xinit directly and set up XAUTHORITY on your own.)

Depending on where you're running this script, you may not have the permission to run fgconsole (“Couldn't get a file descriptor referring to the console”), because it needs to be able to open /dev/console and it can't do that if some other user (root, in practice) owns it. I don't know how to find out how to return to the original console otherwise. One solution, if you have root access, would be to replace ORIGINAL_FGCONSOLE=$(fgconsole) … by

ORIGINAL_FGCONSOLE=$(sudo fgconsole)

and give your account the permission to run fgconsole as root by running visudo and adding the line

zzy ALL = (root) NOPASSWD: /bin/fgconsole

after any other line that applies to your account. It's safe barring a bug in the fgconsole program.

¹ If you're using Linux you're using X.org, and if you aren't using Linux there's still a good change that you're using X.org.

Related Question