Tuesday, March 18, 2008

A Mystery: fgconsole and check-foreground-console

Linux, like Unix before it, lets me have several virtual terminals.

On my Ubuntu box, I switch between these with Control-Alt-F[1-7], where Control-Alt-F7 is my default, GUI environment, and the others are garden-variety, full-screen consoles -- tty[1-6].

(The terminals I put up with gnome-terminal are pseudo-ttys: /dev/pts/1 and so on.)

I spend all of my time in the windowing system. The others are primarily useful when my GUI is mis-behaving, though they can also show useful information at boot time.

I can see that Gnome is running on tty7 with ps.
$ ps ajax | grep '[t]ty7'
4879 4884 4884 4884 tty7 4884 Ss+ 0 10:56 /usr/bin/X :0 -br -audit 0 -auth /var/lib/gdm/:0.Xauth -nolisten tcp vt7
If I'm writing a script, can I see which terminal is in the foreground? Yep. fgconsole does the trick.
$ fgconsole
$ sleep 10; fgconsole
$ sleep 20
While I'm on virtual terminal 1, fgconsole runs, and prints a 1.

There's another command in /bin that seems like it could be related, but I can't figure out what it does: check-foreground-console.

There's no man page or usage message, Google turns up nothing useful, and no experiment I've done seems to have any effect on it. It always exits silently with an exit code of 0, though it requires I be root to run it.

If you know what it's about, tell me.

No comments: