2

I'm working on a server and to show detailed GPU information I use these commands:

nvidia-smi
ps -up `nvidia-smi |tail -n +16 | head -n -1 | sed 's/\s\s*/ /g' | cut -d' ' -f3` 

However as you can see, nvidia-smi is called twice. How can I make the output of nvidia-smi go to output and pipe to another command at the same time?

1 Answer 1

5

Use tee:

ps -up `nvidia-smi |tee /dev/stderr |tail -n +16 | head -n -1 | sed 's/\s\s*/ /g' | cut -d' ' -f3` 

Since stdout is piped, you can't make a copy to it, so I picked stderr to show output.

If /dev/stderr is not available, use /proc/self/fd/2.

Sign up to request clarification or add additional context in comments.

3 Comments

I've tested this on the server, and it worked! Thank you very much! Although using /dev/stderr is a little bit weird... Does this command dump to an error log file or something ? I'm sorry I'm not very knowledgeable about Bash
@DangManhTruong Every program has two default output file descriptors, the standard output and the standard error. Usually both are inherited from the parent process, and in your case, the shell, which has both connected to the terminal.
@DangManhTruong "Standard Error" is not any system error log file. It's just another standard output stream for programs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.