You can use watch -g or–chgexit option to exit when the output of the command changes. By default, the watch command displays till the data stream stops or is forced to exit.
Example:
Create and execute a script that inserts 1 item every 2 seconds indefinitely:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
## create a script to insert 1 item every 2 second indefinitly cat << 'EOF' > myscript.sh #!/bin/bash > myfile i=1; while : do echo "$i" >> myfile sleep 2 ((i++)) done EOF ## execute the script in the background chmod +x myscript.sh ./myscript.sh & |
watch -g or –chgexit option example:
1 2 3 4 5 6 7 8 |
## without -g option watch -n 10 tail myfile ## indefinitly returns tail output every 10 seconds ## with -g or --chgexit option watch -g -n 10 tail myfile ## display for 10 seconds and exit as the output changes pkill -f "myscript.sh" ## kill myscript.sh rm my* |