How to save output of “watch” to file [closed]

2020-05-22 00:04发布

I want to run a command every 60 seconds, and save the output to a logfile. I know I can print to console by

watch -n 60 <mycommand>

But what if I want to save it to a file as well as print to console?

2条回答
够拽才男人
2楼-- · 2020-05-22 00:49

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.

查看更多
再贱就再见
3楼-- · 2020-05-22 00:55

Watch is designed to run in a console window. Printing its output to file is inconvenient, because of the extensive amount of unprintable formatting characters.

You can try this without watch, if the exact 60 seconds is not an issue:

 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.

查看更多
登录 后发表回答