Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 6 years ago.
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?
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.
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.