How to use watch command with pipeline on Linux


Command line Basics – Watch

There are several log files in Linux system. Paying close attention to these log files may be one of the important tasks for Linux system administrators. You canusetailcommandEasily view the end of the log file. However, if you want to monitor the file all day, enter it every few minutestailCommand to check the log file is cumbersome. You can write oneWith infinite loopShort ofscriptTo check files regularly, but it turns out that there is already a program that can handle repetitive tasks for you.

Linux watch command

In Linux_watchA kind of Commands provide a way to handle repetitive tasks. By default_watchA kind of The command that follows it will be repeated every two seconds. As you can imagine, monitoring is a good tool for monitoring log files. This is an example.

 watch tail /var/log/syslog

To stop the command execution, just use the standard termination sequence[Ctrl] + C

How to use watch command with pipeline on Linux

Monitoring system logs using Linux watch command

You can send-nSwitch and specify an interval (in seconds) to change the time interval. To check the log file every 10 seconds, try this operation.

 watch -n 10 tail /var/log/syslog

Linux watch command with pipeline

ThewatchThe command is not limited to viewing log files. It can be used to repeat any command you provide. If you willSet to monitor CPU temperature, you can use thewatchCommand passedsensorCommand to view thetemperature

watch -n 1 sensors

The output on my netbook is as follows:

Adapter: Virtual device
temp1:        +45.0°C  (crit = +100.0°C)

I want to filter this output to show only the temperature output and not all the rest.

I can use this command to see it once.

sensors | grep temp | awk '{ print $2 }'

Remember,watchThe command repeats the first command you see. You must be careful when passing one command to the next. You can manage the command pipeline by placing it in quotation marks.

watch -n1 "sensors | grep temp | awk '{ print $2 }'"

How to use watch command with pipeline on Linux

Using the Linux watch command on the pipeline

Use watch as clock

As you may have noticed,watchThe command displays the execution time of the command in the upper right corner of the terminal window. By passing an empty command line parameter, we canwatchUsed as a simple clock. We can include a space in quotation marks to use as an empty command.

watch -n 1 " "

So, as you can see, this gives the command namewatchIt gives another meaning. You can use it like a watch.

Now you know how to use the Linux watch command. What repetitive tasks will you use it for?