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
  • 3
    @usmanayubsh I had also voted to close but I was wrong, it's not a duplicate. The OP is using watch and that makes things more complicated.
    – terdon
    Jan 22 '16 at 16:32
  • 1
    Ok @terdon, I was also having second thoughts. Jan 22 '16 at 16:37
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?.

0
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.