There are some log files in Linux. Observing these log files is an important task of the system administrator. You can easily observe them using the tail command. But if you want to monitor these files for a long time, it is boring to use tail to check those log files every few minutes. You can write a short, infinite loop script to periodically check files, but there is already a program that can handle such repetitive tasks for you.
Linux watch command
The watch command in Linux provides a way to process repeated tasks. By default, watch repeats the command every 2 seconds. Watch is a good tool for observing log files. The following is an example.
watch tail /var/log/syslog
To stop command execution, use the standard kill process [Ctrl] + C.
Monitor syslogs using the Linux watch command
You can use the-n switch to change and specify the time interval. To detect log files every 10 seconds, try this.
watch -n 10 tail /var/log/syslog
Watch command with MPs queue
Watch is not limited to viewing log files. It can be used to repeat any command you give it. If you want to monitor the CPU temperature, you can use watch to keep up with the sensord command.
watch -n 1 sensors
The output on my computer looks like this:
acpitz-virtual-0Adapter: Virtual devicetemp1: +45.0°C (crit = +100.0°C)
I want to filter this output to show only the temperature, but not others.
I can use this command to view
sensors | grep temp | awk '{ print $2 }'
Remember, the watch command repeats the first command after it. Note that the command is followed by the MPs queue. You can manage your commands in quotation marks.
watch -n1 "sensors | grep temp | awk '{ print $2 }'"
Watch command with Pipeline
Watch as the clock
As you have noticed, watch will display the time in the upper right corner of your terminal after it is executed. We can pass an empty command parameter to watch as a simple clock. We can enclose an empty space in quotation marks as an empty parameter.
watch -n 1 " "
As you can see, this gives this command another meaning, watch ). You can use it as your watch.
Now you know how to use the watch command in Linux. What repeated tasks do you want to use it to process?
Recommended reading:
Linux traffic monitoring tool-iftop
Linux top commands
Top commands in Linux
Efficient use of top commands in Linux
Linux top commands
Linux System Monitoring load top Command details