4

I wrote a simple script to monitor my gpu clocks and temps. I wanted it to, aside from being visible on the terminal, log its output on an external file. How could I achieve this? My script below, for reference:

#!/bin/bash

watch -n 1 "amdconfig --odgc" "amdconfig --odgt"

The question is not a duplicate because I'm using the "watch" command.

2 Answers2

4

You really don't want to do that. watch is designed to be, um, watched. Its output is formatted in such a way as to make redirecting it impractical. While you can actually do it using something like tee, the output will be many blank lines and very little useful information.

So, in the specific case of watch, you're better off writing your own little script that does the same thing:

#!/bin/bash

This is an infinite loop, the script will run until you kill it

while :; do

Run the command whose output you want to monitor

amdconfig --odgc" "amdconfig --odgt

Wait for 2 seconds. This mimics what the watch command does

and also avoids spamming your cpu.

sleep 2 done

Save that script as ~/bin/amdwatch or whatever you like, make it executable (chmod +x ~/bin/amdwatch) and then redirect its output to a file:

~/bin/amdwatch > amdwatch.log 

That will run the script until you stop it manually and execute the amdwatch command every two seconds, saving its output to amdwatch.log.

For most scripts, what you are looking for is script.sh > outputFile.txt or script.sh >> outputFile.txt. See How do I save terminal output to a file?.

terdon
  • 100,812
2

watch will by default clear the screen each time it prints to stdout. So I would suggest you use a while loop with a delay. Then you could use script command or tee to log the output. For instance,

while [ 1 ]; do df ; sleep 0.25; done | tee -a MY_FILE

It both will be displayed on the screen and will go to file, appending each run of the command

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497