TOPIC: PIPELINE
Keeping a graphical eye on CPU temperature and power consumption on the Linux command line
20th March 2025Following my main workstation upgrade in January, some extra monitoring has been needed. This follows on from the experience with building its predecessor more than three years ago.
Being able to do this in a terminal session keeps things lightweight, and I have done that with text displays like what you see below using a combination of sensors
and nvidia-smi
in the following command:
watch -n 2 "sensors | grep -i 'k10'; sensors | grep -i 'tdie'; sensors | grep -i 'tctl'; echo "" | tee /dev/fd/2; nvidia-smi"
Everything is done within a watch
command that refreshes the display every two seconds. Then, the panels are built up by a succession of commands separated with semicolons, one for each portion of the display. The grep
command is used to pick out the desired output of the sensors
command that is piped to it; doing that twice gets us two lines. The next command, echo "" | tee /dev/fd/2
, adds an extra line by sending a space to STDERR output before the output of nvidia-smi
is displayed. The result can be seen in the screenshot below.
However, I also came across a more graphical way to do things using commands like turbostat
or sensors
along with AWK programming and ttyplot
. Using the temperature output from the above and converting that needs the following:
while true; do sensors | grep -i 'tctl' | awk '{ printf("%.2f\n", $2); fflush(); }'; sleep 2; done | ttyplot -s 100 -t "CPU Temperature (Tctl)" -u "°C"
This is done in an infinite while
loop to keep things refreshing; the watch
command does not work for piping output from the sensors
command to both the awk
and ttyplot
commands in sequence and on a repeating, periodic basis. The awk
command takes the second field from the input text, formats it to two places of decimals and prints it before flushing the output buffer afterwards. The ttyplot
command then plots those numbers on the plot seen below in the screenshot with a y-axis scaled to a maximum of 100 (-s
), units of °C
(-u
) and a title of CPU Temperature (Tctl)
(-t
).
A similar thing can be done for the CPU wattage, which is how I learned of the graphical display possibilities in the first place. The command follows:
sudo turbostat --Summary --quiet --show PkgWatt --interval 1 | sudo awk '{ printf("%.2f\n", $1); fflush(); }' | sudo ttyplot -s 200 -t "Turbostat - CPU Power (watts)" -u "watts"
Handily, the turbostat
can be made to update every so often (every second in the command above), avoiding the need for any infinite while
loop. Since only a summary is needed for the wattage, all other output can be suppressed, though everything needs to work using superuser privileges, unlike the sensors
command earlier. Then, awk
is used like before to process the wattage for plotting; the first field is what is being picked out here. After that, ttyplot
displays the plot seen in the screenshot below with appropriate title, units and scaling. All works with output from one command acting as input to another using pipes.
All of this offers a lightweight way to keep an eye on system load, with the top
command showing the impact of different processes if required. While there are graphical tools for some things, command line possibilities cannot be overlooked either.
Another way to supply the terminal output of one BASH command to another
26th April 2023My usual way for sending the output of one command to another is to be place one command after another, separated by the pipe (|
) operator, adjusting the second command as needed. However, I recently found that this approach does not work well for docker pull
commands until I uncovered another option.
The solution is to enclose the input command in $( )
within the output command. Within the parentheses, any kind of command can be declared and includes anything with piping as part of it. As long as text is being printed to the terminal, it can be fed to the second command and used as required. Thus, you can have something like the following:
docker pull $([command outputting name of image to download])
This approach has helped with other kinds of automation of docker image and container use and deployment because it is so general. There may be other uses found for the approach yet.
The power of pipes
12th July 2007One of the great features of the UNIX shell is that you can send the output from one command to another for further processing. Take the following example for instance:
ls -l | grep "Jul 12"
This takes the long directory file listing output and sends it to grep
for subsetting (all files created today in this example) before it is returned to the screen. The |
character is the pipe trigger, and you can have as many pipes in your command as you want, though readability may dictate how far you want to go.
Using SAS FILENAME statement to extract directory file listings into SAS
30th May 2007The filename statement's pipe
option allows you to direct the output of operating system commands into SAS for further processing. Usefully, the Windows dir
command (with its /s switch) and the UNIX and Linux equivalent ls
allow you to get a file listing into SAS. For example, here's how you extract the list of files in your UNIX or Linux home directory into SAS:
filename DIRLIST pipe 'ls ~';
data dirlist;
length filename $200;
infile dirlist length=reclen;
input buffer $varying200. reclen;
run;
Using the ftp
option on the filename statement allows you to get a list of the files in a directory on a remote server, even one with a different operating system to that used on the client (PC or server), very useful for cases where cross-platform systems are involved. Here's some example code:
filename dirlist ftp ' ' ls user='user' host='host' prompt;
data _null_;
length filename $200;
infile dirlist length=reclen;
input buffer $varying200. reclen;
run;
The PROMPT option will cause SAS to ask you for a password, and the null string is where you would otherwise specify the name of a file.