watch命令是為命令行輸出設計的工具,其結果包含很多不可打印的字符,所以輸出重定向到文件中很不方便,比如這樣做的話有很多亂碼:
(watch -n 60 <mycommand> ) >> file.log
所以可以的解決方法有兩個
- 把輸出結果的語句寫到command里面
比如監控GPU的顯存變化並寫入日志
watch -n 3 'nvidia-smi -q -d MEMORY|tee -a gpu.log'
- 寫腳本
while <some condition>
do
<mycommand> 2>&1 | tee -a /path/to/logfile
sleep 60
done
使用tee命令而不是重定向的原因是它可以將內容輸出到標准輸出設備的同時保存成文件