GNU/Linux >> Linux Esercitazione >  >> Linux

guarda esempi di comandi per eseguire un comando ripetutamente o monitorare file modificabili dinamicamente (come /proc/*)

Il comando Watch è uno strumento davvero accurato ed è utile in molte situazioni. Il comando watch può essere utilizzato per monitorare periodicamente qualsiasi file o script. Viene eseguito ogni 2 secondi per impostazione predefinita e funzionerà fino all'interruzione.

# 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 sintassi di base del comando watch è:

# watch [-n seconds] [-d] [command]

Ecco,

-d flag will highlight the differences between successive updates.
-n flag is to specify the interval. The default value is 2 seconds.

Ecco un esempio di output:

# 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

Ecco,

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.

Esempio 1:monitoraggio di un file che cambia dinamicamente come /proc/meminfo

C'è un modo per monitorare qualsiasi file sul sistema con il comando watch.

Il comando:

# watch -n 10 -d  cat /proc/meminfo

produrrebbe un output dello stato di meminfo ogni 10 secondi sullo schermo e evidenzierà eventuali modifiche.

Esempio 2:cerca la modifica nel contenuto di una directory

Un altro uso eccellente del comando watch è tenere d'occhio il contenuto della directory e vedere se viene aggiunto o rimosso un nuovo file.

# watch -d ls -lt

-lt switch nel comando ls mostra l'ultimo file modificato in alto.

Esempio 3:rimozione del titolo/intestazione dall'output.

Nel caso in cui non desideri stampare l'intestazione nell'output del comando watch, puoi utilizzare l'opzione –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

Esempio 4:evidenziazione della differenza cumulativa

Nel caso in cui desideri evidenziare la differenza cumulativa nell'output, puoi utilizzare l'opzione -d=cumulative nel comando. Ad esempio :
L'output dopo l'aggiunta di un nuovo file – new_file1 :

L'output dopo l'aggiunta di un altro nuovo file – new_file2 :


Linux
  1. Importanti file di filesystem Linux /proc che devi conoscere

  2. Esempi pratici di comando rsync in Linux

  3. Esempi di comandi diff in Linux

  4. Quando dovrei usare /dev/shm/ e quando dovrei usare /tmp/?

  5. I siti web dovrebbero vivere in /var/ o /usr/ in base all'utilizzo consigliato?

Cos'è il comando Linux Watch + Esempi

Comando Linux watch con esempi

lsof Command in Linux (10 esempi)

10 esempi di comando Linux ss per monitorare le connessioni di rete

/proc/cpuinfo e /proc/meminfo in Linux

Comprendere i file /proc/mounts, /etc/mtab e /proc/partitions