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?
the shortcut is Ctrl + Shift + S ; it allows the output to be saved as a text file, or as HTML including colors!
If you want to send the output to a file and see it on the screen, use the tee command instead of the > and >> operators. To use the tee command you must pipe the output from the command you're running, like ls, to the tee command, using the pipe operator, a vertical bar (|).
To do that, press the “File” button in the menu bar and click “Save output as … “ This will open a window dialog where you can save the terminal output. From there, you can pick where you want to save the file.
By default, the watch command updates the output every two seconds. Press Ctrl+C to exit out of the command output. The watch command is useful when you need to monitor changes in a command output over time.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With