The watch command can execute a command repeatedly, with a default time interval of 2 seconds.
Watchifconfig can monitor network status every two seconds.
Watch-d ifconfig can monitor network status every two seconds and mark the changed parts
Watch-d ifconfig eth0 can monitor eth0 only every two seconds and mark the changed part.
Watch-d’ifconfig eth0 | grep bytes’can monitor eth0 only every second, show only the traffic part, and mark the changed part.
Watch can also be used to view changes in files, such as:
For example, watch ls-la file, which means two seconds to see how file size changes in file1.
Exit: Ctrl + C
Examples of watch commands
Watch is a very useful command. Basically all Linux distributions come with this widget. Like the name, watch can help you monitor the results of a command and save you from running it manually over and over again. Under Linux, watch is the next program that executes periodically and displays the results of execution on a full screen. You can use it to monitor the results of all the commands you want, such as tail a log file, ls to monitor the size of a file, see your imagination!
1. Command format
Watch [parameter] [command]
2. Command Function
The output results of commands can be output to standard output devices, mostly for periodic execution of commands/timing execution of commands.
3. Command parameters
– N or — interval watch runs the program every 2 seconds by default, and you can use – N or – interval to specify the interval time.
– D or – differences highlights the changed area with the – D or – differences option watch. The – D = cumulative option highlights the changes (whether or not they have changed in the latest one).
– t or – no – Title closes the time interval at the top of the watch command, the command, and the output of the current time.
– h, – help to view help documents
4. Use examples
Command: Highlight changes in the number of network links every second
watch -n 1 -d netstat -ant
Every 1.0s: netstat -ant Mon Feb 27 20:49:38 2017
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 192.168.0.210:22 192.168.0.5:51577 ESTABLISHED
tcp6 0 0 :::80 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
Description: Other operations:
Handover terminal: Ctrl + X
Exit watch: Ctrl + G
Example 2: Highlighting the number of HTTP links every second
watch -n 1 -d 'pstree|grep http'
Highlight the number of HTTP links every second. If the following command has a pipe character, you need to add”to reorganize the command area.
Example 3: Real-time view of the number of connections established by simulated attack clients
Watch'netstat-an | grep: 21 | grep < client IP > | wc-l'
Example 4: Monitoring changes in scf’s files in the current directory
watch -d 'ls -l|grep scf'
Example 5:10 second output system average load
watch -n 10 'cat /proc/loadavg'
The output results are as follows:
Every 10.0s: cat /proc/loadavg Mon Feb 27 20:54:45 2017
0.01 0.04 0.05 1/132 6335