Another possibility is to use command
to demote exec
from a special builtin to a plain old builtin like:
alias shh='command exec >/dev/null 2>&1'
So now you can do:
(shh; call some process &)
I've just noticed that command
does not work in zsh
(as it seems to do in most other shells), but where it doesn't work you can do instead:
alias shh='eval "exec >/dev/null 2>&1"'
...which should work everywhere.
In fact, you might even do:
alias shh='command exec >"${O:-/dev/null}" 2>&1'
So you could do:
O=./logfile; (shh;echo can anyone hear &)
O=; (shh; echo this\? &)
cat ./logfile
OUTPUT
can anyone hear
Following up a comment discussion with @vinc17, it's worth noting that almost all of a GUI app's console output is generally intended for X
's tty - its console. When you run an X
app from an X
.desktop
file the output it generates is routed to X
's virtual terminal - which is whatever tty it was from which you launched X
in the first place. I can address this tty number with $XDG_VTNR
.
Strangely though - and maybe because I just started using startx
- I can no longer seem to just write to /dev/tty$XDG_VTNR
. This may also (as I think is more likely) have something to do with the very recent and drastic change implemented with Xorg
v1.16 that allows it to run under a systemd
user session rather than requiring root privileges.
Still, I can do:
alias gui='command exec >/dev/tty$((1+$XDG_VTNR)) 2>&1'
(gui; some x app &)
Now all of some x app
's console output is being routed to /dev/tty$((1+$XDG_VTNR))
rather than my xterm
's pty. I can get the last page of this at any time like:
fmt </dev/vcs$((1+$XDG_VTNR))
It is probably best practice to dedicate some virtual terminal to log output anyway. /dev/console
is generally already reserved for this, though you may prefer not to do the chown
that is likely required for you to blithely write to that. You may have some function that enables you do a printk
- which is basically printing to /dev/console
- and so could use it that way I suppose.
Another way to do this would be to dedicate a pty to such purposes. You could, for instance, keep an xterm
window open, save the output of tty
when run from there in an environment variable, and use that value as the destination for gui
's output. In that way all of the logs would be routed to a separate log window, that you could then scroll through if you liked.
I once wrote an answer about how a similar thing could be done with bash
history, if you're interested.
$DISPLAY
is not set (e.g. if the user forgot a-X
for ssh) or an X authorization problem like here: http://unix.stackexchange.com/questions/108679/x-client-forwarded-over-ssh-cannot-open-display-localhost11-0 – vinc17 Aug 18 '14 at 01:18"$@" 2>&1 | { quit=$(($(date +%s)+5)); while read line && [ $(date +%s) -lt $quit ]; do printf "[%s] %s\n" "$(date +%T)" "$line"; done; } | head -n 10 &
(the most important point was the idea, not the actual implementation). – vinc17 Aug 18 '14 at 14:54"$@"
works with bash, dash, ksh, and zsh, at least. And$DISPLAY
may be set but non-working. And some X apps report errors to stderr, e.g. xterm. – vinc17 Aug 18 '14 at 15:49evince --presntation
, I want to see the error instead of waiting and wondering why nothing shows up. I don't see the point in hiding such errors. – vinc17 Aug 18 '14 at 16:01