Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save output of "watch" to file [closed]

Tags:

linux

bash

ubuntu

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?

like image 654
eran Avatar asked Feb 18 '14 09:02

eran


People also ask

How do I save output to a file?

the shortcut is Ctrl + Shift + S ; it allows the output to be saved as a text file, or as HTML including colors!

How do you pipe an output to a file?

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 (|).

How do you save a terminal input to a file?

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.

How do you get out of watch command?

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.


1 Answers

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.

like image 123
sweetkorn Avatar answered Oct 01 '22 12:10

sweetkorn