Sunday, March 24, 2019

Executing Commands Repeatedly



While I've been using Linux pretty regularly since '98, I only recently became acquainted with the 'watch' command.  The primary purpose of this utility is to repeatedly run a command, at a specified frequency, and display the results.

Suppose you're writing to a file and wish to monitor the file size as it goes along.  Whelp, you could issue 'ls -l somefile' repeatedly, write a simple script or make use of the watch command.

If you wish to issue an 'ls -l somefile' every 5 seconds, the syntax is:


$ watch -n 5 'ls -l somefile'



If find this particularly of use if I am monitoring the disk usage of a file or file system.


$ watch -n 5 'du -sh .'

$ watch -n 5 'df -h /'



I find this quite useful on any number of occasions, especially when you wish the output displayed.


If the output isn't particularly interesting, perhaps you only need to run an identical command X times to perform run-time comparisons then in-line bash scripting is your friend;



$ for i in $(seq 1 5); do ls -l somefile; done



Anything worth doing once is worth doing N times.

No comments:

Post a Comment