Watch Command es una herramienta muy buena y resulta útil en muchas situaciones. El comando watch se puede usar para monitorear cualquier archivo o script periódicamente. Se ejecuta cada 2 segundos de forma predeterminada y se ejecutará hasta que se interrumpa.
# watch -h Usage: watch [-dhntv] [--differences[=cumulative]] [--help] [--interval=[n]] [--no-title] [--version] [command] -d, --differences[=cumulative] highlight changes between updates (cumulative means highlighting is cumulative) -h, --help print a summary of the options -n, --interval=[seconds] seconds to wait between updates -v, --version print the version number -t, --no-title turns off showing the header
La sintaxis básica del comando watch es:
# watch [-n seconds] [-d] [command]
Aquí,
-d flag will highlight the differences between successive updates. -n flag is to specify the interval. The default value is 2 seconds.
Aquí hay una salida de muestra:
# watch -n 10 -d ls -lt Every 10.0s: ls -lt Tue Feb 14 12:27:43 2017 total 0 -rw-r--r-- 1 root root 0 Feb 14 12:27 new_file_just_created -rw-r--r-- 1 root root 0 Feb 14 10:46 file1 -rw-r--r-- 1 root root 0 Feb 14 10:46 file2 -rw-r--r-- 1 root root 0 Feb 14 10:46 file3
Aquí,
Every 10.0s : is the time interval to run the watch command. i.e. 10 seconds. ls -lt : is the command to be executed every 10 seconds. Tue Feb 14 12:27:43 2017 : is the current date and time.
Ejemplo 1:Monitoreo de un archivo que cambia dinámicamente como /proc/meminfo
Existe una manera de monitorear cualquier archivo en el sistema con el comando watch.
El comando:
# watch -n 10 -d cat /proc/meminfo
produciría una salida del estado de meminfo cada 10 segundos en la pantalla y resaltará si hay algún cambio.
Ejemplo 2:busque el cambio en el contenido de un directorio
Otro uso excelente del comando watch es vigilar el contenido del directorio y ver si se agrega o elimina algún archivo nuevo.
# watch -d ls -lt
El interruptor -lt en el comando ls muestra el último archivo modificado en la parte superior.
Ejemplo 3:Eliminar el título/encabezado de la salida.
En caso de que no desee imprimir el encabezado en la salida del comando watch, puede usar la opción –no-title o -t.
# watch -t -d ls -lt total 0 -rw-r--r-- 1 root root 0 Feb 14 10:47 new_file_just_created -rw-r--r-- 1 root root 0 Feb 14 10:46 file1 -rw-r--r-- 1 root root 0 Feb 14 10:46 file2 -rw-r--r-- 1 root root 0 Feb 14 10:46 file3
Ejemplo 4:Resaltar la diferencia acumulada
En caso de que desee resaltar la diferencia acumulada en la salida, puede usar el modificador -d=acumulativo en el comando. Por ejemplo:
La salida después de agregar un nuevo archivo – new_file1:
La salida después de agregar otro archivo nuevo – new_file2 :