4

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?)

  • What you're calling “window” is in fact called “virtual console” or “virtual terminal” (“vt” for short). It doesn't make sense to me to call them “window”, even if I infer the meaning from context. – Gilles 'SO- stop being evil' Oct 31 '16 at 22:39
  • What operating system are you using? – Gilles 'SO- stop being evil' Oct 31 '16 at 22:41
  • @Gilles Thanks for correcting me! Yes I indeed don't know how to call it. And I'm using Ubuntu 14.04. – zzy Nov 1 '16 at 5:13
1

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.

  • Thank you for the script! I acknowledge that I'm a newbie for xserver and couldn't understand it well. $(fgconsole) leads to "Couldn't get a file descriptor referring to the console" on my machine. And besides this error msg, when I run the script following your guidance, I jumped to a new vt (the new term I learnt just now), and blocked forever at "waiting for X server to shut down (EE) Server terminated successfully (0). Closing log file." I find that, I can use ctrl+alt+f7 to go back to the original vt, and ctrl+alt+f8 to go into this blocking vt. Is this expected? – zzy Nov 1 '16 at 5:30
  • I'm using the default Ubuntu 14.04 (I believe I didn't install a new xserver). As far as I'm concerned, it should be xorg (please correct me if I'm wrong here). Also, is it possible to get the file name of the new vt (the second part of my question)? I suspect $(fgconsole) may have sth to do with this. – zzy Nov 1 '16 at 5:39
  • @zzy fgconsole returns the number of the current console, but it doesn't always work when you're under X, only if you ran startx and told the X server to use the same console (or more generally a console that you were logged into), as opposed to telling the X server to use a new console or using an X display manager. I made a few tweaks to the script, in particular the message “New X session …” was going to a file instead of the original terminal, but if fgconsole doesn't work where you're starting, I don't know how to find out how to get back without running fgconsole as root. – Gilles 'SO- stop being evil' Nov 1 '16 at 22:24
  • Thank you! Yes $(fgconsole) works well with sudo. And I also add sudo for the one after X_FGCONSOLE= in your command. But the line adding to /etc/sudoers by visudo seems to be incorrect, the file will corrupt after adding this line (I have to revert it: askubuntu.com/questions/73864/…). Also, the new vt shows .xinitrc not found, which lies at the last line of your script. I double checked and I indeed don't have such file. Do I miss anything here? – zzy Nov 2 '16 at 6:04
  • @zzy .xinitrc is the file that runs your user session. If you don't have one and then the system file /etc/X11/xinit/xinitrc is used instead. For the sudoers line, I'd forgotten to put the absolute path, but if you use visudo then it detects syntax errors and refuses to change the configuration, no recovery is needed. – Gilles 'SO- stop being evil' Nov 2 '16 at 12:15

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.