Stand With Ukraine

Monday, March 2, 2015

Linux: execute a command periodically and watch the result

I am aware of cron and it does a great job when you want it to do something for you weekly, daily or with whatever frequency. But you have to configure it and stop it, and so on and so on. But what if you just want to quickly see something to start happening or the dynamics of something that is already happening and then just move on (for example new files start appearing in the folder). I needed this functionality because a model, that I use, stores the last executed time step in a text file and updates it after each time step (updates, so 'tail -f fname' does not help). What I was doing is executing 'cat fname' million times and frequently until I have found out about the 'watch -n 1 cat fname' (http://en.wikipedia.org/wiki/Watch_(Unix)), which does it for me every second and nicely updates the result. So I decided to write it here for my (and for anyone's) reference.