简体   繁体   English

将Xterm的输出重定向到日志文件

[英]Redirect output of Xterm to log file

I have not been able to find an answer to this problem. 我无法找到此问题的答案。 I have three programs kicked off in xterm for a demonstration automation script. 我在xterm中启动了三个用于演示自动化脚本的程序。 I need to log the results of the xterm windows. 我需要记录xterm窗口的结果。 Right now, my script looks as follows: 现在,我的脚本如下所示:

#!/bin/bash/sh

echo "NOTE - THIS PROCESS TAKES APPROXIMATELY 60 SECONDS TO RUN"

    cd ~/myProject/ProgramOne
    xterm -e ProgramOne progone.config 2>&1 /tmp/logs/p1.txt &

    cd ~/myProject/ProgramTwo
    xterm -e ProgramOne progtwo.config 2>&1 /tmp/logs/p2.txt &

    cd ~/myProject/ProgramThree
    xterm -e ProgramOne progthree.config 2>&1 /tmp/logs/p3.txt &


# allow the scripts to collect data
sleep 60

# kill the xterm sessions from running since this is just a demonstration
pkill -9 xterm

echo "******************************************"
echo "START PROGRAMS SCRIPT COMPLETE"
echo "******************************************"   

I have verified that ~/myProject/ProgramOne and ~/myProject/ProgramTwo and ~/myProject/ProgramThree all exist, as does /tmp/logs/* 我已验证~/myProject/ProgramOne~/myProject/ProgramTwo~/myProject/ProgramThree都存在, /tmp/logs/*

The files get created, they just simply have nothing in them and do NOT contain the output of the xterm windows, which I can see popup with hundreds of lines of information. 创建文件后,它们只是没有任何内容,并且不包含xterm窗口的输出,我可以看到其中包含数百行信息的弹出窗口。

xterm doesn't write to stdout; xterm不写到stdout; rather it displays the output of whatever command it invokes in its window. 而是在窗口中显示其调用的任何命令的输出。 You need to redirect the output of ProgramOne , not the output of xterm . 您需要重定向ProgramOne的输出,而不是xterm的输出。

And something I didn't notice when I initially wrote this answer: your redirection is incorrect. 最初写此答案时,我没有注意到:您的重定向不正确。 You're passing /tmp/logs/p1.txt as an argument to xterm . 您将/tmp/logs/p1.txt作为参数传递给xterm The 2>&1 redirects stderr to stdout, but does not take a file name argument. 2>&1将stderr重定向到stdout,但不使用文件名参数。 (Yes, you can mix redirections with program arguments. For example, echo >hello.out hello will write hello to hello.out ) (是的,您可以将重定向与程序参数混合​​使用。例如, echo >hello.out hello会将hello写入hello.out

For example change 例如变化

xterm -e ProgramOne progone.config 2>&1 /tmp/logs/p1.txt &

to

xterm -e 'ProgramOne progone.config >/tmp/logs/p1.txt 2>&1' &

(I'm assuming that you want to send ProgramOne 's stdout and stderr to tmp/logs/p1.txt .) (我假设您想将ProgramOne的stdout和stderr发送到tmp/logs/p1.txt 。)

(You could also tell xterm to log its output to a file, using the -l option, but that might be disabled and the output could include formatting characters that you probably don't want.) (您还可以使用-l选项,告诉xterm将其输出记录到文件中,但是可能会禁用它,并且输出中可能包含您可能不需要的格式化字符。)

The following change in the command worked: 命令中的以下更改有效:

xterm -e ProgramOne progone.config 2>&1 /tmp/logs/p1.txt &

to

xterm -e 'ProgramOne progone.config 2>&1 | tee /tmp/logs/p1.txt' &

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM