The watch command is very good, but it can only be used under Linux systems. Can it be used under Windows? Watch is often used to monitor system status information. Is there an equivalent command under Windows?

The watch command is a utility for monitoring the output of a command or script. It will run the specified command periodically and display the output of the command on the terminal in real time. Through the watch command, you can easily monitor the execution of a command or dynamically view the output changes of a command.

The syntax of the watch command is as follows:

watch [选项] 命令

Among them, the option can be one of the following common parameters:

  • -n Or  --interval: Specify the refresh interval time in seconds, the default value is 2 seconds.
  • -d Or  --differences: Highlight the changed parts of the output.
  • -t Or  --no-title: Do not display header information at the top of the output results.

Here are some example uses of the watch command:

  1. Monitor changes in the current system time:
watch date
  1. Monitor the current system load every 5 seconds:
watch -n 5 uptime
  1. View system disk usage in real time:
watch df -h
  1. Monitor changes in a log file:
watch tail -n 10 logfile.txt

If you still have questions about the watch command, you can use man watch to query, as shown below:

The watch command is really easy to use, especially in some system performance monitoring scenarios. For myself, the most frequent use of the watch command is to monitor the server GPU utilization information.

The nvidia-smi command is the abbreviation of NVIDIA System Management Interface (NVIDIA System Management Interface), which is a command line tool used to monitor and manage NVIDIA GPU (graphics processor). Through the nvidia-smi command, you can obtain system information, video memory usage, process information, etc. related to NVIDIA GPU.

When it comes to querying and monitoring GPU information, the nvidia-smi command is naturally indispensable. When using the nvidia-smi command, the general syntax is as follows:

nvidia-smi [选项]

Here are some common options and usage examples:

  1. Display the status and related information of all GPUs in the current system:
nvidia-smi
  1. Regularly refreshes the display of GPU status, updating every 2 seconds:
nvidia-smi -l 2
  1. Display GPU process information line by line:
nvidia-smi pmon
  1. View details for a specified GPU:
nvidia-smi -i <GPU 索引>

Among them, <GPU 索引> is the index number of the GPU, starting from 0 and increasing.

  1. Output the GPU memory usage and process usage to a file:
nvidia-smi --query-gpu=memory.used,pid,process_name --format=csv > gpu_info.txt

These examples show some common uses of the nvidia-smi command. If you need more information about the nvidia-smi command, you can  nvidia-smi --help view the help documentation of the command by running.

The commands I often use myself are as follows:

watch -n1 -d nvidia-smi

The meaning of this command is to automatically query the GPU status every 1s and display the differences. The display effect is as follows:

It is very convenient to use under Linux, but one worry is whether it can be used so conveniently under Windows?

Let’s take a look:

Obviously watch is not a command that can be recognized under windows.

However, you can query the GPU information by executing nvidia-smi directly in the cmd terminal, as shown below:

But if you want to be able to implement automatic interval query like under the Linux system, it will not work. It is really inconvenient. Here I am wondering if this function can also be implemented through scripts or terminal commands. I checked some According to the data, it is found that it is indeed possible to automatically execute specified commands at specified intervals. I will not go into details here. The detailed implementation is given directly, as shown below:

@ECHO OFF


SET ExecuteCommand=nvidia-smi


SET ExecutePeriod=5


SETLOCAL EnableDelayedExpansion

:loop

  cls

  echo !date! !time!
  echo After !ExecutePeriod! s will execute the cmd^: !ExecuteCommand!

  echo.

  %ExecuteCommand%
  
  timeout /t %ExecutePeriod%

goto loop

The execution result is as follows:

Dynamic countdown at the bottom, what I set here is to automatically execute nvidia-smi, which is the GPU information query command, after an interval of 5 seconds.

If you don't want to display the countdown information at the bottom and want the effect to be highly similar to the effect under the Linux system, it is also possible.

The following script can be used as shown below:

@ECHO OFF


SET ExecuteCommand=nvidia-smi


SET ExecutePeriod=5


SETLOCAL EnableDelayedExpansion

:loop

  cls

  echo !date! !time!
  echo After !ExecutePeriod! s will execute the cmd^: !ExecuteCommand!

  echo.

  %ExecuteCommand%
  
  timeout /t %ExecutePeriod% > nul

goto loop

"timeout /t %ExecutePeriod% > nul" is a command for Windows systems that waits for a specified period of time before continuing to execute subsequent commands. With this command, you can create delays or implement time-based operations in batch scripts or command prompts.

Specifically, "%ExecutePeriod%" in the command is a variable used to specify the waiting time, in seconds. The command will pause for the specified time when executing this line, and then continue executing subsequent commands. The "> nul" part is used to redirect the output of the command to a null device so that no wait time information is displayed on the screen.

This command is typically used when you need to introduce a delay in your script or execute commands at certain intervals.

The terminal display results are as follows:

If you are interested, you can try it yourself and take a note!

Guess you like

Origin blog.csdn.net/Together_CZ/article/details/132812010