watch (Unix)
watch is a command line program and belongs to the Linux packages procps and procps-ng . It starts another Unix command or a command sequence at regular short intervals and transfers the result to the standard output , i.e. it normally displays it in the terminal so that changes can be observed.
Watch is defined as a C program with a source code of approx. 700 lines.
Working method
By default, watch starts the specified command or the specified command sequence every 2 seconds. While watch is working, it uses the entire terminal for its display, i.e. the terminal content is apparently deleted. Watch is usually terminated with the key combination [Ctrl] + [C], after which the terminal displays its previous content again.
If you want a different repetition rhythm, you can specify this in seconds with the call parameter -n
or --interval
and a specification immediately following it. The shortest possible repetition interval is a tenth of a second. It should be noted that with fractions of a second the spelling depends on the localization of the system, that is, in an English / American shell you write, for example watch -n 0.4 …
, in a shell with German localization, on the other hand, watch -n 0,4 …
or shorter watch -n,4 …
.
watch calls for the execution of the commands that have been passed to it /bin/sh
or /bin/dash
, therefore watch does not know the aliases that are usually /bin/bash
valid in the shell session from which watch was started. To make the effect of an alias available for watch, you have to write what the alias stands for in a separate shell script file, which has the same name as the alias. This shell script must then be saved in a directory in the search path, for example in /usr/local/bin
.
service
Calling syntax
watch [Optionen] [']Kommando [Kommandooptionen][']
Call arguments
The Unix parameters and GNU options in the following table have the same function; it is up to you whether you want to use the short or long form.
Unix parameters | GNU option | effect |
---|---|---|
-d
|
--differences | Highlights changes since the last pass. |
-h
|
--help | Displays the quick help with all possible call parameters. |
-n Sek.
|
--interval | Specifies the waiting time between program calls in seconds. |
-t
|
--no-title | Makes watch not display a header |
-v
|
--version | Displays the version of watch. |
Application examples
The watch command is useful for observing changes, for example when you repeatedly call the command ls -l
to track the change in the size of a file or directory, or when watch, as in the following example, is called repeatedly ps
to keep certain processes running can be observed.
Show all running instances of a program
$ watch "ps -ef | grep -e '\<apache' -e '^UID\>' | grep -Ev '\<(grep|watch)\>'"
This command requests a list of the processes every two seconds , but only keeps those lines in which there is a word beginning with "apache" or a line beginning with the word "UID", then removes all lines in which the word " grep ”or“ watch ”occurs, and finally displays the remaining lines selected in this way on the screen.
The output can look like this:
Alle 2,0s: ps -ef | grep -e '\<apache' -e '^UID\>' | grep -Ev '\<(grep|watch)\>' Sat May 26 07:03:45 2018
UID PID PPID C STIME TTY TIME CMD root 1555 1 0 Mai09 ? 00:01:07 /usr/sbin/apache2 -k start www-data 10927 1555 0 Mai25 ? 00:00:00 /usr/sbin/apache2 -k start www-data 10928 1555 0 Mai25 ? 00:00:32 /usr/sbin/apache2 -k start www-data 10929 1555 0 Mai25 ? 00:00:32 /usr/sbin/apache2 -k start
Web links
- watch (1) manual page in English
- Procps - The / proc file system utilities (English)
- procps-ng / procps (English speaking)
Individual evidence
- ↑ procps-ng / procps. Accessed June 5, 2018 .