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
echo "New X session running on vt$X_FGCONSOLE" >"$ORIGINAL_TTY"
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
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
¹ 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.