loading...

Linux Mint – Using the watch command

How to build a Docker Compose YAML files

Another command that is very useful for system administration is the watch command. Even better is the fact that the watch command is extremely easy to learn as what it does is simple. It repeats the command typed immediately after it every two seconds by default. For example, you can view the output of the ls command every two seconds by typing the following command:


watch ls /home/user

This is not very exciting, is it? To illustrate what ls does, open another terminal window. In that window, create a new file in your home directory. You should see the new file appear in the output of the first window. If you delete the file, you’ll see the file disappear from the watch output.

Note

To break out of the watch command, press Ctrl + C on your keyboard.

While this may be useful for monitoring a single directory, it may not be a very exciting example. For an even better example, try the following command:


sensors

As long as your motherboard supports it, the sensors command will output the current temperature of your CPU. Some (but not all) computers will display the fan rotation speed as well. You’ve just printed the temperature of your CPU to your terminal. However, it would be more useful to have the output automatically updated without having to execute the sensors command again. As you would have guessed, the watch command can automatically update the output of sensors every two seconds. This can be achieved using the following command line:


watch sensors

In the following screenshot, the watch and sensors commands are used to monitor the system temperature:

Now, this is more useful. If you were running a very CPU-intensive program and wanted to keep an eye on the temperature of your processor, this is one way to do so. However, of course, it gets better. Two seconds is still a bit of a delay. Let’s update the output more frequently using the following command line:


watch -n 1 sensors

By adding the -n option, we can change the number of seconds in which the output of watch updates. In the preceding example, we tell the watch command that we’d like it to update every second rather than the default two seconds. Now, the output of the sensors command is much more useful.

You can use the watch command against virtually any shell command with varying results. In fact, there may be a time where even updating every second is too slow. Take the following command for example:


date

The date command simply prints the current date and time. If you were to do so with the watch command with updates every second, which is shown in the following command line, the results may not be very reliable:


watch -n 1 date

Every second is pretty close, but not exact to the clock. You aren’t limited to seconds with the watch command. You can update in fractions of seconds as well, using the following command:


watch -n 0.1 date

With the preceding command, our output is closer to real time. It may not be 100 percent perfect, but it’s good enough to suit any purpose that would require monitoring the time.

Comments are closed.

loading...