我正在使用以下命令打开终端并执行脚本。如何将输出保存到日志文件。
gnome-terminal -x bash -c "print1.py; read -n1" &
我尝试了以下选项,但没有成功。请提出解决方案。
gnome-terminal -x bash -c "print1.py; read -n1" & > log.txt
gnome-terminal -x bash -c "print1.py; read -n1" & | tee log.txt
gnome-terminal -x bash -c "print1.py; read -n1" & | tee -a log.txt
答案1
假设read
只是为了交互,请尝试保存命令内的输出bash
:
gnome-terminal -x bash -c "print1.py | tee log.txt; read -n1" &
答案2
只需将script
所有输出保存到文件即可。
[morris@rhel tmp]$ script output.log
Script started, file is output.log
[morris@rhel tmp]$ uname -r
3.10.0-514.21.1.el7.x86_64
[morris@rhel tmp]$ exit
Script done, file is output.log
[morris@rhel tmp]$ cat output.log
Script started on Sun 18 Jun 2017 06:55:39 PM HKT
[morris@rhel tmp]$ uname -r
3.10.0-514.21.1.el7.x86_64
[morris@rhel tmp]$ exit
Script done on Sun 18 Jun 2017 06:55:48 PM HKT
[morris@rhel tmp]$
有了gnome-terminal
,类似下面的方法可能会有效。
gnome-terminal -x bash -c "script -c 'print1.py; read -n1' output.log; who -a;" &