I want to run a command every 60 seconds, and save the output to a logfile. I know I can print to console by
我希望每60秒运行一个命令,并将输出保存到日志文件中。我知道我可以打印到控制台
watch -n 60 <mycommand>
But what if I want to save it to a file as well as print to console?
但是,如果我想将它保存到一个文件中,并将其保存到控制台呢?
2 个解决方案
#1
13
Watch is designed to run in a console window. Printing it's output to file is inconvenient, because of the extensive amount of unprintable formatting characters.
Watch被设计成在控制台窗口中运行。打印它的输出到文件是不方便的,因为大量的不可打印格式字符。
You can try this without watch, if the exact 60 seconds is not an issue:
如果60秒不是个问题,你可以不戴手表试试:
while <some condition>
do
<mycommand> 2>&1 | tee -a /path/to/logfile
sleep 60
done
This saves the output to a log file and shows it on console as well.
这将输出保存到日志文件中,并在控制台显示它。
#2
4
try it:
试一试:
while true
do
watch -n 60 <command> 2>&1 | tee -a logfile
done
I use tee
so that you can see the output on your terminal as well as capture it in your log.
我使用tee,以便您可以看到终端上的输出,并将其捕获到日志中。
#1
13
Watch is designed to run in a console window. Printing it's output to file is inconvenient, because of the extensive amount of unprintable formatting characters.
Watch被设计成在控制台窗口中运行。打印它的输出到文件是不方便的,因为大量的不可打印格式字符。
You can try this without watch, if the exact 60 seconds is not an issue:
如果60秒不是个问题,你可以不戴手表试试:
while <some condition>
do
<mycommand> 2>&1 | tee -a /path/to/logfile
sleep 60
done
This saves the output to a log file and shows it on console as well.
这将输出保存到日志文件中,并在控制台显示它。
#2
4
try it:
试一试:
while true
do
watch -n 60 <command> 2>&1 | tee -a logfile
done
I use tee
so that you can see the output on your terminal as well as capture it in your log.
我使用tee,以便您可以看到终端上的输出,并将其捕获到日志中。