Friday, May 3, 2024
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 3832  / 2 Years ago, fri, february 4, 2022, 12:14:34

gnu watch is a very useful tool for inspecting a program output: It executes the program and shows the output full-screen every 2 seconds.



Sometimes, I don't want the previous output to be erased, but rather be printed line by line with a time stamp. For that, I use bash scripts like:



while true; 
do echo -n "`date` ";
ssh ubuntu@server -o ConnectTimeout=1 "uptime" ;
sleep 1;
done


Is there a watch-like tool that can run a command and display its output with a timestamp in a line without erasing previous output?


More From » command-line

 Answers
6

I'd say you've found it in simple loops but you could do a number of things from here:


Write a function to handle that for you



function uberwatch {
# call: uberwatch <interval> <command>
while true; do
"${@:2}";
sleep $1;
done
}

You could lodge that somewhere around your ~/.bashrc.


Log the output to file but keep viewing with watch


watch ... "command | tee -a watchlog.log"

You'd still only see the latest run-through but you could dig through a historical log if you needed to.


[#24030] Saturday, February 5, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gavgenerati

Total Points: 120
Total Questions: 126
Total Answers: 119

Location: Marshall Islands
Member since Wed, Feb 9, 2022
2 Years ago
;