Reputation: 11892
I need to repeatedly execute a command (e.g. ls) on a linux OS and I also would like to know how long it has been running since I started watching it.
I know I can use watch
to execute the command. My question is how I can show when the "watch" was started? Or how long it has been watching?
Upvotes: 0
Views: 464
Reputation: 198456
It is not supported out of the box, but you can hack around it by supplying a more complex command.
watch bash -c '"echo $(($(date +%s) - '$(date +%s)'))s; echo ---; ls"'
Instead of directly watching the command you want (ls
in this toy example), watch a bash
instance, because it can parse a command line and do more. We tell this bash
to execute three commands, the first of which calculates the difference between the seconds since epoch at watch
invocation and the seconds since epoch at bash
invocation. The second prints a line because for prettiness, and the third then executes the desired command.
Showing when the watch started follows the same idea, but is simpler:
watch bash -c "'echo $(date); echo ---; ls'"
Note that the order of the quotes is not random. The last command could have also been written similar to the above one:
watch bash -c '"echo '$(date)'; echo ---; ls"'
Upvotes: 1