0

I use the nvidia-smi command quite frequently, and I have a separate alias in my .bashrc that I use to monitor it (alias gpu='watch -n 3 nvidia-smi').

I recently learned about customizing the output message of nvidia-smi and am using the following: nvidia-smi | tee /dev/stderr | awk '/ C / {print $3}' | xargs -r ps -up that I got from this Stack Overflow question.

I'd like to replace the original nvidia-smi command in my watch alias, but am wondering how I can do so. Simply replacing it doesn't work, and I've tried surrounding the new command in quotation marks but that leads to the original nvidia-smi message along with a

error: user name does not exist

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).

right below it.

How should I go about achieving what I want? Any help is appreciated. Thanks.

Sean
  • 2,890
  • 8
  • 36
  • 78
  • Is running `nvidia-smi | tee /dev/stderr | awk '/ C / {print $3}' | xargs -r ps -up` without watch successful ? – Philippe Mar 27 '21 at 11:23
  • Yes, that prints out the message I want. It prints out the original output of `nvidia-smi` and has rows below that with columns being usernames, commands run, etc. – Sean Mar 27 '21 at 13:38
  • What is your new alias ? – Philippe Mar 27 '21 at 13:42
  • Right now it loos like this: `"watch -n 3 \"nvidia-smi | tee /dev/stderr | awk '/ C / {print $3}' | xargs -r ps -up\""`. I've tried removing the quotations around the `nvidia-smi` full command, but that also doesn't work. – Sean Mar 27 '21 at 22:06

1 Answers1

1

Try this to see if it produces expected result :

alias gpu='watch -n 1 "nvidia-smi | tee /dev/stderr | awk '"'"'/ C / {print \$3}'"'"' | xargs -r ps -up"'

'"'"' is to escape single quote inside single quotes.

Philippe
  • 20,025
  • 2
  • 23
  • 32
  • Is there any way to pipe the output of this into a `tail` command if I wanted to see only the bottom portion of the output? – Sean Mar 27 '21 at 22:49
  • Try to remove `tee /dev/stderr |` – Philippe Mar 27 '21 at 22:54
  • Removing that portion leads to an `error: user name does not exist` message without the output of `nvidia-smi`. Right now I tried running the alias `alias gpu='watch -n 1 "nvidia-smi | awk '"'"'/ C / {print \$3}'"'"' | xargs -r ps -up | tail -n $(($LINES - 2))"'` – Sean Mar 27 '21 at 22:57
  • You need to put `\$((LINES - 2))` – Philippe Mar 27 '21 at 23:17
  • Unfortunately that didn't work. :( I'll figure it out. – Sean Mar 28 '21 at 00:13