user1765709
user1765709

Reputation: 451

how to use xterm to spawn a process in another terminal?

In my bash script, I am trying to run a process in another terminal window using following:

xterm -e /home/mohit/a.out

Now I want to redirect the output using tee command as follows:

xterm -e /home/mohit/a.out 2>&1 | tee logfile

But I am not getting anything in logfile. Any clues, why is this happening ?

Upvotes: 0

Views: 4502

Answers (2)

Olaf Dietsche
Olaf Dietsche

Reputation: 74108

@peteches already showed, how to capture the output by giving the whole command line to -e.

You can also ask xterm to capture all output to a logfile with -l

xterm -l -e /home/mohit/a.out

which logs to a file called XtermLog.XXXXXX or Xterm.log.hostname.yyyy.mm.dd.hh.mm.ss.XXXXXX

You can specify the logfile with -lf

xterm -l -lf logfile -e /home/mohit/a.out

The drawback is, this doesn't work with existing files. You must remove logfile, before xterm will use it.

Upvotes: 1

peteches
peteches

Reputation: 3629

Looks like xterm spawns a new window and executes -e in that window. once that command has completed it exits.

Because it's opened up in a new shell it's stderr stdout etc are attached to that window.

If what you want is to open that window and keep it open teeing the log file you need to quote everything you want to be executed by xterm, otherwise bash will interpret the redirections and pipes itself. eg

xterm -e /home/mohit/a.out 2>&1 | tee logfile

will be translated by bash into:

use xterm to execute the command /home/mohit/a.out then redirect stderr to stdout and pipe into tee.

As xterm spawns new window with new stdout etc. the tee doesn't get any input an therefore nothing in logfile

Try

xterm -e '/home/mohit/a.out 2>&1 | tee logfile'

which will force xterm to execute the whole pipeling

Upvotes: 4

Related Questions