The grabconsole program()

The grabconsole program

    grabconsole file [command ...]

grabconsole redirects messages to /dev/console to the given file, which must be the slave side of a pseudoterminal. It may be specified as either a filename or a file descriptor. Then it execs the given command, if any.

This is useful to log such messages to disk when syslogd is not running but some services log through syslog.

    $ cat /service/console-log/run
    #!/bin/sh
    exec \
    ptyget \
    sh -c '
    exec 6<&- 7<&- &&
    setblock +4 &&
    grabconsole 5 &&
    exec <&4 \
    setuidgid loguser \
    multilog t ./main'

ptyget allocates a pseudoterminal. setblock sets the master side to be blocking (so a process reading the master side will wait for input instead of getting EAGAIN when there is currently no input to be read). grabconsole redirects /dev/console messages to the slave side of the pseudoterminal. multilog, running as loguser, reads console messages from the master side of the pseudoterminal and writes them to ./main. Note that this may cause problems with console logins unless logins use a different device referring to the console, such as /dev/tty1 on Linux.

xterm -e sh -ec 'grabconsole 0; setblock +0; exec cat' is equivalent to xconsole.