GICT knowledge base
Linux watch command
Published
Watch command is one of the most useful commands which mainly helps to keep an update on various system administrative processes. Today’s Internet facility provides for the hackers to intrude into any system which is not well secured and regularly monitored for such attacks. These all can be monitored through the log files which are updated regularly. These regularly updated logs can be checked by executing the commands frequently. Since it is a tedious task to execute the commands repeatedly to view the logs. Commands such as head, tail, cat etc. which are used to read the files are usually used to check the logs. This is where the importance of watch commands comes into the picture. The watch command allows you to regularly repeat a command and produce the output. The burden of frequently executing the commands is avoided for the system administrators.
Watch Command
The syntax of watch command is:
watch [options] command
The running period of watch command is by default 2 seconds. The running time cycle is counted between the full execution of the command and start of next execution.
Examples:
1) The given example provides updated error/notices/warning every 2 seconds.
$ watch tail /var/log/messages
Every 2.0s: tail /var/log/messages Thu Oct 13 07:31:16 2016
Oct 13 05:01:01 centos7 systemd: Started Session 87 of user root.
Oct 13 05:01:01 centos7 systemd: Starting Session 87 of user root.
Oct 13 05:29:09 centos7 kernel: conftest[30674]: segfault at 0 ip 0000000000400670 sp 00007ffdef8232a0 error 4 in conftest[400000+1000]
Oct 13 05:29:09 centos7 kernel: conftest[30697]: segfault at 0 ip 00007fc9ca9faad6 sp 00007ffce4c92bd8 error 4 in libc-2.17.so[7fc9ca8c8000+1b7000]
Oct 13 05:31:03 centos7 kernel: conftest[9223]: segfault at 0 ip 0000000000400670 sp 00007ffe3868d8e0 error 4 in conftest[400000+1000]
Oct 13 05:31:03 centos7 kernel: conftest[9246]: segfault at 0 ip 00007fccefa5bad6 sp 00007ffc02d18548 error 4 in libc-2.17.so[7fccef929000+1b7000]
2) The given example allows checking the usage of the disk where the result is updated every 2 seconds.
$ watch df -h
Every 2.0s: df -h Thu Oct 13 07:34:56 2016
Filesystem Size Used Avail Use% Mounted on
/dev/vda3 47G 2.4G 42G 6% /
devtmpfs 888M 0 888M 0% /dev
tmpfs 897M 0 897M 0% /dev/shm
tmpfs 897M 28M 869M 4% /run
tmpfs 897M 0 897M 0% /sys/fs/cgroup
/dev/vda1 477M 234M 215M 53% /boot
tmpfs 180M 0 180M 0% /run/user/0
3) The below-given example updated uptime and load
$ watch uptime
Every 2.0s: uptime Thu Oct 13 07:40:15 2016
07:40:15 up 3 days, 7:54, 1 user, load average: 0.00, 0.01, 0.05
4) The below-given example provides an updated queue size of Exim
$ watch exim -bpc
Every 2.0s: uptime Thu Oct 13 07:40:15 2016
Iteration delay
watch [-n <seconds>] command
This option allows modifying the running period of watch command. The given example shows how to run w command with a running period of 4 seconds.
watch –n 4 date
$ watch -n 4 w
Every 4.0s: w Thu Oct 13 07:56:49 2016
07:56:49 up 3 days, 8:11, 1 user, load average: 0.02, 0.04, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 201.38.64.185 04:46 33.00s 0.09s 0.00s watch -n 4 w
Watch help
To get all the information regarding the watch command use the option –h or –help
$ watch –h [or –help]
Watch version
watch -v [–version]
This option allows getting the version of the watch.
$ watch -v
watch from procps-ng 3.3.10
If you need any further assistance please contact our support department.
Related guides
How to Restrict an SSH Key to One Command
Restrict an SSH key to one command with authorized_keys options. Protect the command and check that unwanted shell and forwarding access are denied.
Linux and CommandsSelf-Hosted Video Processing With FFmpeg: A Practical Guide
Learn how to self-host video processing on a VPS using FFmpeg, with practical commands for conversion, compression, thumbnails, and automated workflows.
Linux and CommandsHow to use nohup: Commands in Linux That Survive Terminal Closes
Keep commands running after you close the terminal. nohup examples with wget, Python, rsync, and Docker on any VPS.
Linux and CommandsHow to Fix “Too Many Open Files” Error on Linux Servers
Learn how to fix the "Too Many Open Files" error on Linux servers by understanding file descriptors, adjusting limits, and diagnosing leaks effectively.
