Linux Commands – watch
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed touch command in Linux which is used to update the access and modification times of a file to the current time.
https://cloudaffaire.com/linux-commands-touch/
In this blog post, we will discuss the watch command in Linux. The watch command can be used to run a command repeatedly, displaying its output and errors (the first screenful). This allows you to watch the command output change over time. By default, the command is run every 2 seconds and display output indefinitely until interrupted.
Linux Commands – watch:
You can use watch command to run a command repeatedly displaying the output and errors.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
############################ ## Linux Commands | watch ## ############################ ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ##------ ## watch ##------ ## watch [option] command ## create a script to insert 1 item every 2 second indefinitly cat myscript.sh ----------------------- #!/bin/bash > myfile i=1; while : do echo "$i" >> myfile sleep 2 ((i++)) done ----------------------- ## execute the script in the background chmod +x myscript.sh ./myscript.sh & watch tail myfile ## displays the output of tail command ## every 2 seconds interval ## press control + c to exit watch date ## can output any command |
You can use watch -d or –differences [permanent] options to highlight the differences between successive updates. watch -differences [permanent] option changes highlight to be permanent, allowing to see what has changed at least once since the first iteration.
1 2 3 4 5 6 |
## watch -d or --differences=permanent options watch -d tail myfile ## highlight differences between successive updates watch --differences=permanent tail myfile ## highlight what changed at least once since the first iteration |
You can use watch -n seconds or –interval seconds options to specify update interval. The command will not allow quicker than 0.1 second interval, in which the smaller values are converted. The default update interval of the watch command is 2 seconds.
1 2 3 |
## watch -n seconds or --interval seconds options watch -n 10 tail myfile ## refresh the output every 10 seconds |
You can use watch -t or –no-title options to turn off the header showing the interval, command, and current time at the top of the display, as well as the following blank line.
1 2 3 |
## watch -t or --no-title options watch -t tail myfile ## only display the output without any additional details |
You can use watch -e or –errexit options to freeze updates on command error, and exit after a keypress. By default, watch command will continue to update on command error and does not exit until interrupted.
1 2 3 4 5 6 7 |
## watch -e or --errexit options watch cat some_error ## returns an error ## press control + c to exit watch -e cat some_error ## returns an error ## press any key to exit |
You can use watch -g or –chgexit options to exit when the output of command changes. This is very useful to track command depending upon the change in output. For example, you can track connectivity using ping command and take action depending upon the output.
1 2 3 4 5 |
## watch -g or --chgexit options watch -n 10 tail myfile ## indefinitely returns tail output every 10 seconds watch -g -n 10 tail myfile ## display for 10 seconds and exit as the output changes |
You can use watch -x or –exec options to run the command on sh -c.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
## watch -x or --exec options ## create a function and export myfun() { echo 'hello world'; } export -f myfun myfun ## returns hello world watch myfun ## returns hello world watch --exec myfun ## returns error watch --exec bash -c "myfun" ## returns hello world |
You can use watch -c or –color to interpret ANSI color and style sequences. By default, ANSI color and style sequences are not interpreted.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
## watch -c or --color options pkill -f "myscript.sh" ## kill myscript.sh ## modify the script cat myscript.sh ------------------------ #!/bin/bash > myfile for fgbg in 38 48 ; do # Foreground / Background for color in {0..255} ; do # Colors # Display the color printf "\e[${fgbg};5;%sm %3s \e[0m" $color $color >> myfile echo >> myfile sleep 2 done echo >> myfile done ------------------------ ## execute the script in the background ./myscript.sh & watch tail myfile ## does not interpret ANSI color watch -c tail myfile ## interpret ANSI color pkill -f "myscript.sh" unset -f myfun rm my* |
Hope you have enjoyed this article. In the next blog post, we will discuss wc command in Linux.